-
Notifications
You must be signed in to change notification settings - Fork 17
/
day08.py
executable file
·83 lines (64 loc) · 1.01 KB
/
day08.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
from utils.all import *
advent.setup(2020, 8)
fin = advent.get_input()
timer_start()
lines = get_lines(fin)
prog = []
for l in lines:
op, n = l.split()
n = int(n)
prog.append((op, n))
pc = 0
acc = 0
seen = set()
while 1:
if pc in seen:
break
seen.add(pc)
op, arg = prog[pc]
print(pc, op, arg)
if op == 'acc':
acc += arg
pc += 1
elif op == 'nop':
pc += 1
elif op == 'jmp':
pc = pc + arg
advent.submit_answer(1, acc)
i = 0
while i < len(prog):
# print(i)
prog2 = prog[:]
op, arg = prog2[i]
if op == 'nop':
prog2[i] = ('jmp', arg)
elif op == 'jmp':
prog2[i] = ('nop', arg)
done = 0
acc = 0
seen = set()
pc = 0
while 1:
if pc in seen:
break
seen.add(pc)
op, arg = prog2[pc]
# print(pc, op, arg)
if op == 'acc':
acc += arg
pc += 1
elif op == 'nop':
pc += 1
elif op == 'jmp':
pc = pc + arg
if pc > len(prog2):
break
elif pc == len(prog2):
done = 1
break
if done:
break
i += 1
# 1210 wrong
advent.submit_answer(2, acc)