-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
41 lines (35 loc) · 1.13 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
import downloadInput as aoc
input=[]
file = "day%d/input.txt" % aoc.day
for line in open(file, "r").readlines():
cmd, val = tuple(line.split(" "))
input.append([cmd, int(val)])
def program(input):
acc =0
instruction = 0
executed = []
while instruction not in executed and instruction < len(input):
executed.append(instruction)
# instructions = ["nop", "acc", "jmp", ]
if input[instruction][0] == "nop":
instruction = instruction+1
elif input[instruction][0] == "acc":
acc = acc + input[instruction][1]
instruction = instruction+1
elif input[instruction][0] == "jmp":
instruction = instruction + input[instruction][1]
else:
print("Unhandled instruction, you suck")
return acc, not instruction < len(input), executed
acc, done, executed = program(input)
print("not pog", acc)
# part2
executed = [x for x in executed if (input[x][0]!="acc" or input[x][0]=="jmp")]
for i in executed:
if done:
print("pog", acc)
break
else:
instruction, input[i][0] = input[i][0], (input[i][0] == "nop" and "jmp" or "nop")
acc, done, ex = program(input)
input[i][0] = instruction