adventofcode2024/day17/sol.py

57 lines
1.1 KiB
Python
Raw Normal View History

2024-12-17 06:43:51 +00:00
sample = [0,1,5,4,3,0]
input = [2,4,1,1,7,5,4,4,1,4,0,3,5,5,3,0]
def simulate(a, prog, check=False):
b = 0
c = 0
ip = 0
out = []
while ip < len(prog):
op = prog[ip]
r = prog[ip+1]
if op in (0,2,5,6,7):
# combo op
if r == 4:
r = a
elif r == 5:
r = b
elif r == 6:
r = c
elif r == 7:
die
else:
pass # literal
if op == 0: # adv
a = a>>r
elif op == 1: # bxl
b ^= r
elif op == 2: # bst
b = r%8
elif op == 3: # jnz
if a != 0:
ip = r - 2
elif op == 4: # bxc
b ^= c
elif op == 5: # out
out.append(r%8)
if check and out[-1] != prog[len(out)-1]:
return out
elif op == 6:
b = a>>r
elif op == 7:
c = a>>r
ip += 2
print(",".join(str(x) for x in out))
return out
simulate(729, sample)
simulate(46337277, input)