Files
aocpy/2024/d17.py
2024-12-18 22:09:38 -05:00

62 lines
1.3 KiB
Python

from lib import get_data
from lib import ints
data = get_data(__file__)
nums, prog = data.split("\n\n")
regs = ints(nums)
prog = ints(prog)
i = 0
out = []
while i < len(prog) - 1:
inst, op = prog[i], prog[i + 1]
assert op != 7
combo = op if op not in [4, 5, 6] else regs[op - 4]
literal = op
match inst:
case 0:
regs[0] = regs[0] >> combo
case 1:
regs[1] = regs[1] ^ literal
case 2:
regs[1] = combo % 8
case 3:
if regs[0] != 0:
i = literal - 2
case 4:
regs[1] = regs[1] ^ regs[2]
case 5:
out.append(combo % 8)
case 6:
regs[1] = regs[0] // (2**combo)
case 7:
regs[2] = regs[0] // (2**combo)
case _:
assert False
i += 2
print(",".join(map(str, out)))
def get_a(a_current, xs):
if len(xs) == 0:
return [a_current // 8]
x = xs[0]
aan = []
for a_new in range(8):
a = a_current + a_new
b = a % 8
assert b == a_new
b = b ^ 1
c = a >> b
b = b ^ c
b = b ^ 5
if (b % 8) == x:
aan += get_a(a << 3, xs[1:])
return aan
aa = get_a(0, list(reversed(prog)))
print(min(aa))