aoc2020/08/1.py
2020-12-08 22:10:59 +00:00

38 lines
656 B
Python

instructions = [l.split(" ") for l in open("input", "r").read().split("\n") if l]
instructions = [(operation, int(argument)) for (operation, argument) in instructions]
visited = set()
pc = 0
ac = 0
operations = {}
def operation(op):
def wrap(func):
operations[op] = func
return wrap
@operation("acc")
def acc(p):
global pc
global ac
ac += p
pc += 1
@operation("jmp")
def jmp(p):
global pc
pc += p
@operation("nop")
def nop(p):
global pc
pc += 1
while pc not in visited:
visited.add(pc)
(opcode, p) = instructions[pc]
print(pc, ac, opcode, p)
op = operations[opcode]
op(p)
print(ac)