-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
68 lines (45 loc) · 973 Bytes
/
08.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from lib import *
input = read_input(2020, 8)
lines = input.splitlines()
acc = 0
pc = 0
seen = set()
while pc < len(lines):
if pc in seen:
break
seen.add(pc)
a, b = lines[pc].split()
if a == "acc":
acc += int(b)
pc += 1
elif a == "jmp":
pc += int(b)
elif a == "nop":
pc += 1
print(acc)
def simulate(flip):
acc = 0
pc = 0
seen = set()
while pc < len(lines):
if pc in seen:
return None
seen.add(pc)
a, b = lines[pc].split()
if pc == flip:
if a == "nop":
a = "jmp"
elif a == "jmp":
a = "nop"
if a == "acc":
acc += int(b)
pc += 1
elif a == "jmp":
pc += int(b)
elif a == "nop":
pc += 1
return acc
for i in range(len(lines)):
if (out := simulate(i)) is not None:
print(out)
break