-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
47 lines (41 loc) · 1.16 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
import sys
from helpers import load_input
def run(codes):
index = 0
acc_value = 0
indexes = []
while index <= len(codes):
try:
inst, step = codes[index].split()
except IndexError:
print('END\n')
return acc_value
if index not in indexes:
indexes.append(index)
else:
print(f"Loop!, last acc: {acc_value}\n")
return False
if inst == 'nop': index = index + 1
elif inst == 'acc':
acc_value += int(step)
index = index + 1
elif inst == 'jmp': index = index + int(step)
def replace_try(codes):
tested = []
x = False
while not x:
for i, code in enumerate(codes):
if 'jmp' in code and i not in tested:
new = codes.copy()
new[i] = code.replace('jmp', 'nop')
tested.append(i)
x = run(new)
if x:
print('ACC:', x)
break
break
if __name__ == '__main__':
codes = load_input('input.txt').splitlines()
print(run(codes))
print('-'*120)
replace_try(codes)