-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
34 lines (29 loc) · 795 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from parse import search
with open("input.txt") as f:
code = [search("{} {:d}", x).fixed for x in f]
def execute(instrs):
visited = set()
pc = 0
acc = 0
while True:
if pc == len(code):
return acc, "OK"
if pc in visited:
return acc, "ERROR"
visited.add(pc)
c, n = instrs[pc]
if c == "acc": acc += n
elif c == "jmp": pc += n-1
elif c == "noop": pass
pc += 1
print(execute(code)[0])
for i in range(len(code)):
c, n = code[i]
if c in ("jmp", "noop"):
code2 = code[:]
if c == "jmp": code2[i] = ("noop", n)
else: code2[i] = ("jmp", n)
res, exit_code = execute(code2)
if exit_code == "OK":
print(res)
break