-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
69 lines (61 loc) · 2.19 KB
/
8.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
57
58
59
60
61
62
63
64
65
66
67
68
69
with open("8.txt") as f:
lines = f.read().splitlines()
instructions= [(line[:3],int(line[4:])) for line in lines]
visited=set()
accCount=0
instrucPointer=0
while not instrucPointer in visited:
if instructions[instrucPointer][0]=="acc":
accCount+=instructions[instrucPointer][1]
visited.add(instrucPointer)
instrucPointer+=1
elif instructions[instrucPointer][0]=="jmp":
visited.add(instrucPointer)
instrucPointer+=instructions[instrucPointer][1]
elif instructions[instrucPointer][0]=="nop":
visited.add(instrucPointer)
instrucPointer+=1
print("Answer part One: ",accCount)
def changeInstruction(index):
if instructions[index][0] == "jmp":
instructions[index] = ("nop",instructions[index][1])
else: instructions[index] = ("jmp",instructions[index][1])
def nowExicutes():
visited2 = set()
instrucPointer2 = 0
while not instrucPointer2 in visited2:
if instructions[instrucPointer2][0] == "acc":
visited2.add(instrucPointer2)
instrucPointer2 += 1
elif instructions[instrucPointer2][0] == "jmp":
visited2.add(instrucPointer2)
instrucPointer2 += instructions[instrucPointer2][1]
elif instructions[instrucPointer2][0] == "nop":
visited2.add(instrucPointer2)
instrucPointer2 += 1
if instrucPointer2 == len(lines):
return True
return False
for v in range(len(lines)):
if instructions[v][0]=="acc":
continue
changeInstruction(v)
if nowExicutes():
print("The bad instruction was ",lines[v]," at ",v+1)
break
changeInstruction(v)
visited=set()
accCount=0
instrucPointer=0
while (not instrucPointer in visited) and instrucPointer<len(lines):
if instructions[instrucPointer][0]=="acc":
accCount+=instructions[instrucPointer][1]
visited.add(instrucPointer)
instrucPointer+=1
elif instructions[instrucPointer][0]=="jmp":
visited.add(instrucPointer)
instrucPointer+=instructions[instrucPointer][1]
elif instructions[instrucPointer][0]=="nop":
visited.add(instrucPointer)
instrucPointer+=1
print("Answer part two: ",accCount)