-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·211 lines (162 loc) · 5.57 KB
/
part_a.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
from abc import ABC
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
1446
"""
runner = Program.from_program_text(_input).to_runner()
runner.run()
return runner.value
class ProgramRunner:
@classmethod
def from_program(cls, program):
instruction_counter = 0
value = 0
return cls(program, instruction_counter, value)
def __init__(self, program, instruction_counter, value,
visited_instruction_indexes=None):
self.program = program
self.instruction_counter = instruction_counter
self.value = value
if visited_instruction_indexes is None:
visited_instruction_indexes = set()
self.visited_instruction_indexes = visited_instruction_indexes
def __repr__(self):
return f"{type(self).__name__}({self.instruction_counter}, {self.value})"
def run(self, prevent_infinite_loop=True):
"""
>>> Program.from_program_text(
... "nop +0\\n"
... "acc +1\\n"
... "jmp +4\\n"
... "acc +3\\n"
... "jmp -3\\n"
... "acc -99\\n"
... "acc +1\\n"
... "jmp -4\\n"
... "acc +6\\n"
... ).to_runner().run()
(True, 5)
"""
while True:
if self.should_exit(prevent_infinite_loop=prevent_infinite_loop):
return self.get_run_return_value()
self.visited_instruction_indexes.add(self.instruction_counter)
self.run_instruction()
def get_run_return_value(self):
return True, self.value
def should_exit(self, prevent_infinite_loop=True):
return prevent_infinite_loop and self.is_in_infinite_loop()
def is_in_infinite_loop(self):
return (
self.instruction_counter
in self.visited_instruction_indexes
)
def run_instruction(self):
instruction = self.program.instructions[self.instruction_counter]
self.instruction_counter, self.value = instruction.run(
self.instruction_counter, self.value)
return self
class Program:
@classmethod
def from_program_text(cls, program_text):
"""
>>> Program.from_program_text(
... "nop +0\\n"
... "acc +1\\n"
... "jmp +4\\n"
... "acc +3\\n"
... "jmp -3\\n"
... "acc -99\\n"
... "acc +1\\n"
... "jmp -4\\n"
... "acc +6\\n"
... )
Program([Nop(0), Acc(1), Jmp(4), Acc(3), Jmp(-3), Acc(-99), Acc(1), Jmp(-4), Acc(6)])
"""
non_empty_lines = filter(None, program_text.splitlines())
return cls(list(map(
Instruction.from_instruction_text, non_empty_lines)))
def __init__(self, instructions):
self.instructions = instructions
def __repr__(self):
return f"{type(self).__name__}({self.instructions})"
def to_runner(self, runner_class=ProgramRunner):
return runner_class.from_program(self)
class Instruction:
key = NotImplemented
instruction_classes = {}
@classmethod
def register(cls, instruction_class):
cls.instruction_classes[instruction_class.key] = instruction_class
return instruction_class
@classmethod
def from_instruction_text(cls, instruction_text):
"""
>>> Instruction.from_instruction_text("nop +0")
Nop(0)
>>> Instruction.from_instruction_text("acc +1")
Acc(1)
>>> Instruction.from_instruction_text("jmp -4")
Jmp(-4)
"""
for instruction_class in cls.instruction_classes.values():
instruction = instruction_class\
.try_from_instruction_text(instruction_text)
if instruction:
return instruction
raise Exception(f"Could not parse '{instruction_text}'")
@classmethod
def try_from_instruction_text(cls, instruction_text):
raise NotImplementedError()
def run(self, instruction_counter, value):
raise NotImplementedError()
class InstructionWithOneIntArgument(Instruction, ABC):
name = NotImplemented
@classmethod
def try_from_instruction_text(cls, instruction_text):
prefix = f"{cls.name} "
if not instruction_text.startswith(prefix):
return None
argument_str = instruction_text[len(prefix):]
argument = int(argument_str)
return cls(argument)
def __init__(self, argument):
self.argument = argument
def __repr__(self):
return f"{type(self).__name__}({self.argument})"
@Instruction.register
class Acc(InstructionWithOneIntArgument):
key = "acc"
name = "acc"
def run(self, instruction_counter, value):
"""
>>> Acc(5).run(0, 0)
(1, 5)
>>> Acc(5).run(0, -5)
(1, 0)
>>> Acc(5).run(0, -10)
(1, -5)
"""
instruction_counter += 1
value += self.argument
return instruction_counter, value
@Instruction.register
class Jmp(InstructionWithOneIntArgument):
key = "jmp"
name = "jmp"
def run(self, instruction_counter, value):
instruction_counter += self.argument
return instruction_counter, value
@Instruction.register
class Nop(InstructionWithOneIntArgument):
key = "nop"
name = "nop"
def run(self, instruction_counter, value):
instruction_counter += 1
return instruction_counter, value
Challenge.main()
challenge = Challenge()