-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
56 lines (46 loc) · 1.35 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
#!/bin/env/python
f = open('day08_input.txt', 'r')
lines = [line.strip() for line in f.readlines()]
run_lines = set()
acc = 0
pointer = 0
while pointer not in run_lines:
line = lines[pointer]
instruction, param = line.split()
param = int(param)
run_lines.add(pointer)
if instruction == 'acc':
acc += param
pointer += 1
elif instruction == 'jmp':
pointer += param
else:
pointer += 1
print(acc)
for change_from, change_to in [('jmp', 'nop'), ('nop', 'jmp')]:
error_lines = [
i
for i, line in enumerate(lines)
if line.split()[0] == change_from]
for error_line_pointer in error_lines:
error_line = lines[error_line_pointer]
run_lines = set()
acc = 0
pointer = 0
while pointer not in run_lines and pointer != len(lines):
line = lines[pointer]
instruction, param = line.split()
param = int(param)
if pointer == error_line_pointer:
instruction = change_to
run_lines.add(pointer)
if instruction == 'acc':
acc += param
pointer += 1
elif instruction == 'jmp':
pointer += param
else:
pointer += 1
if pointer == len(lines):
print(acc)
break