-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
86 lines (71 loc) · 2.33 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import pytest
import sys
# acc -> accumulator
# starts at 0
# jmp -> jump
# number is offset relative to itself
# nop -> no operation, go to instruction directly below
def parse_instruction(instruction):
splits = instruction.split(" ")
arg = splits[0]
num = int(splits[1])
return arg, num
def execute(instructions):
pc = 0
acc = 0
finish_pc = len(instructions)
executed_list = []
while pc not in executed_list:
arg, num = parse_instruction(instructions[pc])
executed_list.append(pc)
if arg == "acc":
acc += num
pc += 1
elif arg == "nop":
pc += 1
elif arg == "jmp":
pc += num
if pc == finish_pc:
print("Program executed successfully")
print(pc, acc, "\n")
return pc, acc
print("Program exiting due to infinite loop\n")
return pc, acc
input_list = []
with open('day8.txt', 'r') as f:
strings = f.read().splitlines()
for item in strings:
if item:
input_list.append(item)
if __name__ == "__main__":
# PART 1
_, acc = execute(input_list)
print("Part 1: {}".format(acc))
# PART 2
correct_pc = len(input_list)
print("Correct PC: {}".format(correct_pc)) #651
print(input_list[0])
for index, ins in enumerate(input_list):
instructions_copy = input_list[:] # force new copy!
arg, num = parse_instruction(ins)
if arg == "acc":
print(f"skip index {index} with instruction {ins}\n")
elif arg == "jmp":
edited_instruction = "{} {}".format("nop", num)
print(f"editing index {index}")
print(f"{input_list[index]}")
print(f"{edited_instruction}")
instructions_copy[index] = edited_instruction
print(input_list[0:10])
print(instructions_copy[0:10])
pc, acc = execute(instructions_copy)
elif arg == "nop":
edited_instruction = "{} {}".format("jmp", num)
print(f"editing index {index}")
print(f"{input_list[index]}")
print(f"{edited_instruction}")
instructions_copy[index] = edited_instruction
print(input_list[0:10])
print(instructions_copy[0:10])
pc, acc = execute(instructions_copy)