-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·259 lines (223 loc) · 8.11 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
import re
from dataclasses import dataclass
from typing import Tuple, Dict
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
3578
"""
return RuleSet.from_rules_text(_input).get_diagnostic_checksum()
@dataclass
class RuleSet:
start_state: str
checksum_step: int
rules: Dict[Tuple[str, int], 'Rule']
re_start_state = re.compile(r"^Begin in state ([A-Z])\.$")
re_checksum_step = re.compile(
r"^Perform a diagnostic checksum after (\d+) steps\.$")
@classmethod
def from_rules_text(cls, rules_text):
"""
>>> RuleSet.from_example_a()
RuleSet(start_state='A', checksum_step=6,
rules={('A', 0): Rule(match=('A', 0), value_write=1, move_offset=1,
next_state='B'),
('A', 1): Rule(match=('A', 1), value_write=0, move_offset=-1,
next_state='B'),
('B', 0): Rule(match=('B', 0), value_write=1, move_offset=-1,
next_state='A'),
('B', 1): Rule(match=('B', 1), value_write=1, move_offset=1,
next_state='A')})
"""
first_block, *rules_blocks = rules_text.strip('\n').split('\n\n')
start_state_line, checksum_step_line = first_block.splitlines()
start_state, = cls.re_start_state.match(start_state_line).groups()
checksum_step_str, = cls.re_checksum_step\
.match(checksum_step_line).groups()
checksum_step = int(checksum_step_str)
rules = {
rule.match: rule
for rule in sum((
StateRule.rules_from_state_rule_text(block)
for block in rules_blocks
), [])
}
return cls(start_state, checksum_step, rules)
@classmethod
def from_example_a(cls):
return cls.from_rules_text(
"Begin in state A.\n"
"Perform a diagnostic checksum after 6 steps.\n"
"\n"
"In state A:\n"
" If the current value is 0:\n"
" - Write the value 1.\n"
" - Move one slot to the right.\n"
" - Continue with state B.\n"
" If the current value is 1:\n"
" - Write the value 0.\n"
" - Move one slot to the left.\n"
" - Continue with state B.\n"
"\n"
"In state B:\n"
" If the current value is 0:\n"
" - Write the value 1.\n"
" - Move one slot to the left.\n"
" - Continue with state A.\n"
" If the current value is 1:\n"
" - Write the value 1.\n"
" - Move one slot to the right.\n"
" - Continue with state A.\n"
)
def get_rule(self, execution):
return self.rules[(execution.state, execution.value)]
def get_diagnostic_checksum(self):
"""
>>> RuleSet.from_example_a().get_diagnostic_checksum()
3
"""
return Execution(self)\
.step_many(self.checksum_step)\
.get_diagnostic_checksum()
class Execution:
def __init__(self, rule_set, tape=None, state=None, position=0):
self.rule_set = rule_set
if tape is None:
tape = {}
self.tape = tape
if state is None:
state = self.rule_set.start_state
self.state = state
self.position = position
def get_diagnostic_checksum(self):
"""
>>> Execution(RuleSet.from_example_a())\\
... .step_many(6).get_diagnostic_checksum()
3
"""
return len(self.tape)
def step_many(self, count):
"""
>>> print("!" + Execution(RuleSet.from_example_a())
... .step_many(6).show(range(-3, 3)))
!... 0 1 1 [0] 1 0 ...
"""
for _ in range(count):
self.step()
return self
def step(self):
"""
>>> rule_set = RuleSet.from_example_a()
>>> print("!" + Execution(RuleSet.from_example_a())
... .step().show(range(-3, 3)))
!... 0 0 0 1 [0] 0 ...
"""
rule = self.rule_set.get_rule(self)
rule.apply(self)
return self
@property
def value(self):
return self.tape.get(self.position, 0)
@value.setter
def value(self, value):
if value not in (0, 1):
raise Exception(f"Expected value to be 0 or 1, not {value}")
if value:
self.tape[self.position] = 1
else:
if self.position in self.tape:
del self.tape[self.position]
def move(self, offset):
self.position += offset
def show(self, _range=None):
"""
>>> print("!" + Execution(RuleSet('A', 6, {})).show(range(-3, 3)))
!... 0 0 0 [0] 0 0 ...
"""
if _range is None:
_range = range(
min(self.position, min(self.tape)),
max(self.position, max(self.tape)) + 1,
)
return "...{}...".format(
"".join(
(
"[{}]"
if self.position == position else
" {} "
).format(self.tape.get(position, 0))
for position in _range
),
)
class StateRule:
re_state = re.compile(r"^In state ([A-Z]):$")
@classmethod
def rules_from_state_rule_text(cls, state_rule_text):
"""
>>> StateRule.rules_from_state_rule_text(
... "In state A:\\n"
... " If the current value is 0:\\n"
... " - Write the value 1.\\n"
... " - Move one slot to the right.\\n"
... " - Continue with state B.\\n"
... " If the current value is 1:\\n"
... " - Write the value 0.\\n"
... " - Move one slot to the left.\\n"
... " - Continue with state B.\\n"
... )
[Rule(match=('A', 0), value_write=1, move_offset=1, next_state='B'),
Rule(match=('A', 1), value_write=0, move_offset=-1, next_state='B')]
"""
first_line, *rule_lines = state_rule_text.splitlines()
state, = cls.re_state.match(first_line).groups()
return [
Rule.from_rule_text(state, "\n".join(rule_lines[start:start + 4]))
for start in range(0, len(rule_lines), 4)
]
@dataclass
class Rule:
match: Tuple[str, int]
value_write: int
move_offset: int
next_state: str
re_current_value = re.compile(r"^\s*If the current value is ([01]):$")
re_value_write = re.compile(r"^\s*- Write the value ([01])\.$")
re_move_offset = re.compile(r"^\s*- Move one slot to the (left|right)\.$")
re_next_state = re.compile(r"^\s*- Continue with state ([A-Z])\.$")
regexes = [re_current_value, re_value_write, re_move_offset, re_next_state]
OFFSET_MAP = {
'left': -1,
'right': 1,
}
@classmethod
def from_rule_text(cls, state, rule_text):
"""
>>> Rule.from_rule_text('A',
... " If the current value is 0:\\n"
... " - Write the value 1.\\n"
... " - Move one slot to the right.\\n"
... " - Continue with state B.\\n"
... )
Rule(match=('A', 0), value_write=1, move_offset=1, next_state='B')
"""
lines = list(rule_text.strip('\n').splitlines())
if len(lines) != len(cls.regexes):
raise Exception(f"Expected {len(cls.regexes)} but got {len(lines)}")
(value_str,), (value_write_str,), (move_offset_str,), (next_state,) = (
regex.match(line).groups()
for regex, line in zip(cls.regexes, lines)
)
value = int(value_str)
match = (state, value)
value_write = int(value_write_str)
move_offset = cls.OFFSET_MAP[move_offset_str]
return cls(match, value_write, move_offset, next_state)
def apply(self, execution):
execution.value = self.value_write
execution.move(self.move_offset)
execution.state = self.next_state
Challenge.main()
challenge = Challenge()