aocpy/2016/d12.py

44 lines
780 B
Python

from lib import *
data = open(0).read().strip()
part_2 = True
REGS = "abcd"
regs = {c: 0 for c in REGS}
if part_2:
regs["c"] = 1
insts = data.splitlines()
i = 0
while i < len(insts):
parts = insts[i].split()
cmd = parts[0]
if cmd == "cpy":
if parts[1] in "abcd":
regs[parts[2]] = regs[parts[1]]
else:
regs[parts[2]] = int(parts[1])
elif cmd == "jnz":
val = 0
if parts[1] in "abcd":
val = regs[parts[1]]
else:
val = int(parts[1])
if val != 0:
i += int(parts[2])
continue
elif cmd == "inc":
regs[parts[1]] += 1
elif cmd == "dec":
regs[parts[1]] -= 1
else:
assert False
i += 1
print(regs["a"])