blob: 37855fcb00f41b10a35e1a94b703467f7ebe908b (
plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import fileinput
import re
from copy import deepcopy
from collections import namedtuple
class Instr(object):
def __init__(self, op, arg):
self.op = op
self.arg = int(arg)
def __repr__(self):
return "{} {}".format(self.op, self.arg)
def run(P, ip, acc):
cmd = P[ip]
if cmd.op == "acc":
acc += cmd.arg
ip += 1
elif cmd.op == "nop":
ip += 1
elif cmd.op == "jmp":
ip += cmd.arg
return (ip, acc)
P = list([Instr(*l.split()) for l in fileinput.input()])
ip = 0
acc = 0
seen = set()
while True:
if ip in seen:
print(acc)
break
seen.add(ip)
ip, acc = run(P, ip, acc)
for change in range(len(P)):
Pmod = deepcopy(P)
if Pmod[change].op == "nop":
Pmod[change].op = "jmp"
elif Pmod[change].op == "jmp":
Pmod[change].op = "nop"
else:
continue
t = 0
ip = 0
acc = 0
while 0 <= ip < len(Pmod) and t < 1000:
t += 1
ip, acc = run(Pmod, ip, acc)
if ip == len(Pmod):
print(acc)
break
|