-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC_Day8_part1.py
59 lines (51 loc) · 1.8 KB
/
AOC_Day8_part1.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
File = open("Day8_Input.txt", "r")
Lines = File.readlines()
def CheckBoot(BootLines):
ExecutedLines = [0]
Accumulator = 0
condition = True
ExecuteLine = 0
while condition == True:
print(ExecuteLine)
Line = BootLines[ExecuteLine]
Commands = Line.split(" ")
Command = Commands[0].strip()
Step = int(Commands[1].strip())
if Command == "nop":
NewExecuteline = ExecuteLine + 1
if Command == "acc":
Accumulator += Step
NewExecuteline = ExecuteLine + 1
if Command == "jmp":
NewExecuteline = ExecuteLine + Step
if NewExecuteline in ExecutedLines:
condition = False
if ExecuteLine == len(BootLines)-1:
break
print("Executeline =", BootLines[ExecuteLine])
print("Accumulator = ", Accumulator)
ExecutedLines.append(NewExecuteline)
ExecuteLine = NewExecuteline
return condition,Accumulator
for i in range(0,len(Lines)):
NewLines = Lines.copy()
if "nop" in Lines[i]:
ChangedLine = Lines[i]
ChangedLine = ChangedLine.strip().replace("nop", "jmp")
NewLines[i] = ChangedLine
condition, Accumulator = CheckBoot(NewLines)
# print(condition,Accumulator)
if condition == True:
print(Accumulator)
break
print(i, condition, Accumulator)
if "jmp" in Lines[i]:
ChangedLine = Lines[i]
ChangedLine = ChangedLine.strip().replace("jmp", "nop")
NewLines[i] = ChangedLine
condition, Accumulator = CheckBoot(NewLines)
# print(condition,Accumulator)
if condition == True:
print(Accumulator)
break
print(i,condition,Accumulator)