-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
48 lines (37 loc) · 1.37 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
def run_machine(code, changing_instr=False, has_instruction_changed=False):
acc, pc = 0, 0
count = {}
while pc < len(code):
instr, val = code[pc]
c = count.get(pc, 0) + 1
if c > 1:
# we are changing instructions and reached infinite loop - terminate
return None if changing_instr else acc
count[pc] = c
if instr == 'acc':
acc += val
pc += 1
elif instr == 'jmp':
if changing_instr and not has_instruction_changed:
code[pc] = ('nop', val)
ret = run_machine(code, True, True)
if ret: return ret
code[pc] = ('jmp', val) # restore instr
pc += val
else: # nop
if changing_instr and not has_instruction_changed:
code[pc] = ('jmp', val)
ret = run_machine(code, True, True)
if ret: return ret
code[pc] = ('nop', val) # restore instr
pc += 1
return acc
def main():
with open("./input.txt", "rt") as f:
data = f.read().splitlines()
data = list(map(lambda s: s.split(' '), data))
data = list(map(lambda s: (s[0], int(s[1])), data))
print(run_machine(data))
print(run_machine(data, True))
if __name__ == '__main__':
main()