-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
91 lines (69 loc) · 2.06 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
87
88
89
90
91
from __future__ import annotations
from typing import NamedTuple, List, Set
class Instruction(NamedTuple):
operation: str
argument: int
@staticmethod
def parse(line: str) -> Instruction:
op, arg = line.split()
return Instruction(op, int(arg))
class Program:
def __init__(self, instructions: List[Instruction]) -> None:
self.instructions = instructions
self.accumulator = 0
self.index = 0
def execute_instruction(self) -> None:
op, arg = self.instructions[self.index]
if op == "nop":
self.index += 1
elif op == "acc":
self.index += 1
self.accumulator += arg
elif op == "jmp":
self.index += arg
def run_one_loop(self) -> None:
executed: Set[int] = set()
while self.index not in executed:
executed.add(self.index)
self.execute_instruction()
def program_ends(self) -> bool:
executed: Set[int] = set()
while self.index not in executed:
if self.index == len(self.instructions):
return True
executed.add(self.index)
self.execute_instruction()
return False
def repair_program(lst_of_instr: List[Instruction]) -> int:
for i, (op, arg) in enumerate(lst_of_instr):
instr = lst_of_instr[:]
if op == "jmp":
instr[i] = Instruction("nop", arg)
elif op == "nop":
instr[i] = Instruction("jmp", arg)
pr = Program(instr)
if pr.program_ends():
return pr.accumulator
return -1
with open("inputs/day8.txt") as f:
instr = [Instruction.parse(x) for x in f.readlines()]
# part 1
p = Program(instr)
p.run_one_loop()
print(p.accumulator)
# part 2
print(repair_program(instr))
# RAW = """nop +0
# acc +1
# jmp +4
# acc +3
# jmp -3
# acc -99
# acc +1
# jmp -4
# acc +6"""
# instructions = [Instruction.parse(x) for x in RAW.split("\n")]
# print(repair_program(instructions))
# program = Program(instructions)
# program.run_one_loop()
# print(program.accumulator)