-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
45 lines (40 loc) · 1.1 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
from aoc import timer, read_file
def parse_data(data):
acc = 0
addresses = set()
address = 0
while address < len(data):
op, arg = data[address].split(' ')
if address in addresses:
raise Exception(f"infinte loop, acc = {acc}")
addresses.add(address)
if op == 'nop':
address += 1
continue
elif op == 'acc':
address += 1
acc += int(arg)
elif op == 'jmp':
address += int(arg)
continue
return acc
@timer
def brute(data):
# brute force corruption until we succeed
for address in range(len(data)):
op, arg = data[address].split(' ')
if op in ['nop', 'jmp']:
new_data = list(data)
new_data[address] = ('nop' if op == 'jmp' else 'jmp') + ' ' + arg
try:
print(parse_data(new_data))
break
except:
pass
if __name__ == "__main__":
data = read_file(8)
try:
print(parse_data(data))
except Exception as e:
print(e)
print(brute(data))