-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathday8.py
61 lines (46 loc) · 1.44 KB
/
day8.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
import copy
ACC = 'acc'
JMP = 'jmp'
NOP = 'nop'
def run(instructions: list) -> [bool, int]:
accumulator = 0
visited = [False for _ in instructions]
ix = 0
while (0 <= ix < len(instructions)) and (not visited[ix]):
visited[ix] = True
operation, arg = instructions[ix]
if operation == ACC:
accumulator += arg
ix += 1
elif operation == JMP:
ix += arg
elif operation == NOP:
ix += 1
is_terminated = ix == len(instructions)
return is_terminated, accumulator
def part1(instructions):
_, accumulator = run(instructions)
return accumulator
def part2(instructions):
for ix, instruction in enumerate(instructions):
operation, arg = instruction
if operation in (NOP, JMP):
instructions_copy = copy.deepcopy(instructions)
instructions_copy[ix][0] = JMP if operation == NOP else NOP
is_terminated, accumulator = run(instructions_copy)
if is_terminated:
return accumulator
return 0
def extract_data(lines: list) -> list:
instructions = []
for line in lines:
operation, arg = line.split(' ')
instructions.append([operation, int(arg)])
return instructions
with open('day8-data.txt') as f:
inputs = [
line
for line in f.read().splitlines()
]
print(part1(extract_data(inputs)))
print(part2(extract_data(inputs)))