Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024/17 #129

Merged
merged 13 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions aoc_wim/aoc2024/q17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
--- Day 17: Chronospatial Computer ---
https://adventofcode.com/2024/day/17
"""
from aocd import data
import re


class Comp:
combo = {4: "A", 5: "B", 6: "C"}

def __init__(self, A, B, C, prog=()):
self.output = []
self.reset(A, B, C, prog)

def __getitem__(self, k):
k = self.combo.get(k, k)
return self.data.get(k, k)

def __setitem__(self, k, v):
k = self.combo.get(k, k)
self.data[k] = v

def reset(self, A=0, B=0, C=0, prog=()):
self.data = {"A": A, "B": B, "C": C}
self.prog = prog
self.ip = 0
self.output.clear()

def adv(self, x):
self["A"] >>= self[x]

def bxl(self, x):
self["B"] ^= x

def bst(self, x):
self["B"] = self[x] & 0o7

def jnz(self, x):
if self["A"]:
self.ip = x - 2

def bxc(self, x):
self["B"] ^= self["C"]

def out(self, x):
self.output.append(self[x] & 0o7)

def bdv(self, x):
self["B"] = self["A"] >> self[x]

def cdv(self, x):
self["C"] = self["A"] >> self[x]

ops = [adv, bxl, bst, jnz, bxc, out, bdv, cdv]

def run(self):
while self.ip < len(self.prog):
opcode, operand = self.prog[self.ip:self.ip+2]
func = self.ops[opcode]
func(self, operand)
self.ip += 2
return ",".join(map(str, self.output))


A, B, C, *prog = map(int, re.findall(r"\d+", data))
comp = Comp(A, B, C, prog=prog)
print("answer_a:", comp.run())

quines = []
stack = [0]
while stack:
A0 = stack.pop()
for n in range(0o10):
A = (A0 << 3) + n
comp.reset(A=A, prog=prog)
comp.run()
if comp.output == prog[-len(comp.output):] and A:
stack.append(A)
if comp.output == prog:
quines.append(A)

print("answer b:", min(quines, default=None))
11 changes: 11 additions & 0 deletions aoc_wim/aoc2024/q17_decompiled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prog = [2, 4, 1, 1, 7, 5, 1, 5, 4, 0, 0, 3, 5, 5, 3, 0]
As = [0o4_532_307_133_267_275]
for A in As:
print(f"{oct(A)}: ", end="")
while A:
B = (A & 7) ^ 0b001
C = A >> B
out = ((B ^ 0b101) ^ C) & 7
print(out, end=",")
A >>= 3
print()
7 changes: 7 additions & 0 deletions tests/2024/17/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Register A: 729
Register B: 0
Register C: 0

Program: 0,1,5,4,3,0
4,6,3,5,6,3,5,2,1,0
-
7 changes: 7 additions & 0 deletions tests/2024/17/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Register A: 2024
Register B: 0
Register C: 0

Program: 0,3,5,4,3,0
-
117440
Loading