-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
55 lines (42 loc) · 1.08 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
from aoc import get_input
instructions = get_input(day=8)
def parse(instructions):
parsed = []
for instruction in instructions:
op, value = instruction.split()
parsed.append([op, int(value)])
return parsed
def run(instructions):
i = 0
acc = 0
executed = set()
while i not in executed and 0 <= i < len(instructions):
executed.add(i)
op, value = instructions[i]
if op == 'nop':
i += 1
elif op == 'acc':
acc += value
i += 1
elif op == 'jmp':
i += value
else:
raise ValueError
return i, acc
# 8-1
i, acc = run(parse(instructions))
print(acc)
# 8-2
mapping = {'nop': 'jmp',
'jmp': 'nop'}
def perturbed(original):
for i, (op, value) in enumerate(original):
if op in mapping:
new = original[:]
new[i] = (mapping[op], value)
yield new
for instructions in perturbed(parse(instructions)):
i, acc = run(instructions)
if i == len(instructions):
print(acc)
break