38 lines
656 B
Python
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)
|