-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·415 lines (331 loc) · 11.5 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python3
import re
from abc import ABC
from dataclasses import dataclass, field
from typing import Dict, List, get_type_hints, Generic, Type, Optional
from aox.challenge import Debugger
from utils import BaseChallenge, PolymorphicParser, TV, get_type_argument_class
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
46065
"""
harness = ConnectionSet.from_connections_text(_input).apply()
if debugger:
print(harness.show())
return harness['a']
@dataclass
class Harness:
wires: Dict[str, int] = field(default_factory=dict)
def __contains__(self, item: str) -> bool:
if not isinstance(item, str):
raise TypeError(f"Expected str but got {repr(item)}")
return item in self.wires
def __getitem__(self, item: str) -> int:
if not isinstance(item, str):
raise TypeError(f"Expected str but got {repr(item)}")
return self.wires[item]
def __setitem__(self, key: str, value: int):
if not isinstance(key, str):
raise TypeError(f"Expected str but got {repr(key)}")
if not isinstance(value, int):
raise TypeError(f"Expected int but got {repr(value)}")
if key in self.wires:
raise Exception(f"Wire {key} already has a value")
self.wires[key] = value
def show(self) -> str:
"""
>>> print(Harness({'a': 1, 'c': 3, 'b': 2}).show())
a: 1
b: 2
c: 3
"""
return "\n".join(
f"{name}: {value}"
for name, value in sorted(self.wires.items())
)
class Value(PolymorphicParser, ABC, root=True):
def can_get_value(self, harness: Harness) -> bool:
raise NotImplementedError()
def get_value(self, harness: Harness) -> int:
raise NotImplementedError()
class LValue(Value, ABC):
pass
class RValue(Value, ABC):
def set_value(self, harness: Harness, value: int):
raise NotImplementedError()
@Value.register
@dataclass(frozen=True, eq=True)
class Wire(LValue, RValue):
name = 'wire'
target: str
re_wire = re.compile(r"^([a-z]+)$")
@classmethod
def try_parse(cls, text: str):
"""
>>> Wire.try_parse('a')
Wire(target='a')
>>> Value.parse('a')
Wire(target='a')
"""
match = cls.re_wire.match(text)
if not match:
return None
target, = match.groups()
return cls(target)
def can_get_value(self, harness: Harness) -> bool:
return self.target in harness
def get_value(self, harness: Harness) -> int:
return harness[self.target]
def set_value(self, harness: Harness, value: int):
harness[self.target] = value
@Value.register
@dataclass(frozen=True, eq=True)
class Constant(LValue):
name = 'constant'
value: int
re_constant = re.compile(r"^(\d+)$")
@classmethod
def try_parse(cls, text: str):
"""
>>> Constant.try_parse('123')
Constant(value=123)
>>> Value.parse('123')
Constant(value=123)
"""
match = cls.re_constant.match(text)
if not match:
return None
value_str, = match.groups()
return cls(int(value_str))
def can_get_value(self, harness: Harness) -> bool:
return True
def get_value(self, harness: Harness) -> int:
return self.value
ConnectionT = TV['Connection']
HarnessT = TV['Harness']
@dataclass
class ConnectionSet(Generic[ConnectionT, HarnessT]):
connections: List[ConnectionT]
@classmethod
def get_connection_class(cls) -> Type[ConnectionT]:
return get_type_argument_class(cls, ConnectionT)
@classmethod
def get_harness_class(cls) -> Type[HarnessT]:
return get_type_argument_class(cls, HarnessT)
@classmethod
def from_connections_text(cls, connections_text: str):
"""
>>> ConnectionSet.from_connections_text("123 -> x\\n123 AND b -> a\\n")
ConnectionSet(connections=[PassThrough(source=Constant(value=123),
destination=Wire(target='x')),
And(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))])
"""
connection_class = cls.get_connection_class()
return cls(list(map(
connection_class.parse, connections_text.splitlines())))
def apply(self, harness: Optional[HarnessT] = None) -> HarnessT:
"""
>>> print(ConnectionSet.from_connections_text(
... "123 -> x\\n"
... "456 -> y\\n"
... "x AND y -> d\\n"
... "x OR y -> e\\n"
... "x LSHIFT 2 -> f\\n"
... "y RSHIFT 2 -> g\\n"
... "NOT x -> h\\n"
... "NOT y -> i\\n"
... ).apply().show())
d: 72
e: 507
f: 492
g: 114
h: 65412
i: 65079
x: 123
y: 456
"""
if harness is None:
harness_class = self.get_harness_class()
harness = harness_class()
remaining = set(self.connections)
while remaining:
applicable = {
connection
for connection in remaining
if connection.can_apply(harness)
}
if not applicable:
raise Exception(
f"Could not find any more applicable to harness {harness} "
f"from {remaining}")
for connection in applicable:
connection.apply(harness)
remaining -= applicable
return harness
class Connection(PolymorphicParser, ABC, root=True):
def get_input_names(self) -> List[str]:
return [
field_name
for field_name, annotation in get_type_hints(type(self)).items()
if isinstance(annotation, type)
and issubclass(annotation, LValue)
]
def get_inputs(self) -> List[Value]:
return [
getattr(self, input_name)
for input_name in self.get_input_names()
]
def can_apply(self, harness: Harness) -> bool:
return all(
_input.can_get_value(harness)
for _input in self.get_inputs()
)
def apply(self, harness: Harness) -> Harness:
raise NotImplementedError()
@dataclass(frozen=True, eq=True)
class UnaryConnection(Connection, ABC):
source: LValue
destination: RValue
re_unary = NotImplemented
RE_UNARY_TEMPLATE = r"^{}([^ ]+) -> ([^ ]+)$"
@classmethod
def try_parse(cls, text: str):
match = cls.re_unary.match(text)
if not match:
return None
source_str, destination_str = match.groups()
return cls(LValue.parse(source_str), RValue.parse(destination_str))
def apply(self, harness: Harness) -> Harness:
value = self.source.get_value(harness)
if not isinstance(value, int):
raise TypeError(
f"Expected int from source {self.source} with {harness}, but "
f"got {repr(value)}")
self.destination.set_value(
harness,
self.apply_to_value(value),
)
return harness
def apply_to_value(self, value: int) -> int:
raise NotImplementedError()
@Connection.register
class Not(UnaryConnection):
"""
>>> Not.try_parse("NOT 123 -> x")
Not(source=Constant(value=123), destination=Wire(target='x'))
>>> Connection.parse("NOT 123 -> x")
Not(source=Constant(value=123), destination=Wire(target='x'))
"""
name = "not"
re_unary = re.compile(UnaryConnection.RE_UNARY_TEMPLATE.format("NOT "))
MASK = 2 ** 16 - 1
def apply_to_value(self, value: int) -> int:
"""
>>> print(Not.parse("NOT x -> h").apply(Harness({'x': 123})).show())
h: 65412
x: 123
"""
return self.MASK & ~value
@Connection.register
class PassThrough(UnaryConnection):
"""
>>> PassThrough.try_parse("123 -> x")
PassThrough(source=Constant(value=123), destination=Wire(target='x'))
>>> Connection.parse("123 -> x")
PassThrough(source=Constant(value=123), destination=Wire(target='x'))
"""
name = "pass-through"
re_unary = re.compile(UnaryConnection.RE_UNARY_TEMPLATE.format(""))
def apply_to_value(self, value: int) -> int:
return value
@dataclass(frozen=True, eq=True)
class BinaryConnection(Connection, ABC):
lhs: LValue
rhs: LValue
destination: RValue
re_binary = NotImplemented
RE_BINARY_TEMPLATE = r"^([^ ]+) {} ([^ ]+) -> ([^ ]+)$"
@classmethod
def try_parse(cls, text: str):
match = cls.re_binary.match(text)
if not match:
return None
lhs_str, rhs_str, destination_str = match.groups()
return cls(
LValue.parse(lhs_str), LValue.parse(rhs_str),
RValue.parse(destination_str))
def apply(self, harness: Harness) -> Harness:
lhs = self.lhs.get_value(harness)
if not isinstance(lhs, int):
raise TypeError(
f"Expected int from lhs {self.lhs} with {harness}, but got "
f"{repr(lhs)}")
rhs = self.rhs.get_value(harness)
if not isinstance(rhs, int):
raise TypeError(
f"Expected int from rhs {self.rhs} with {harness}, but got "
f"{repr(rhs)}")
self.destination.set_value(
harness,
self.apply_to_values(lhs, rhs),
)
return harness
def apply_to_values(self, lhs: int, rhs: int) -> int:
raise NotImplementedError()
@Connection.register
class And(BinaryConnection):
"""
>>> And.try_parse("123 AND b -> a")
And(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
>>> Connection.parse("123 AND b -> a")
And(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
"""
name = "and"
re_binary = re.compile(BinaryConnection.RE_BINARY_TEMPLATE.format("AND"))
apply_to_values = staticmethod(int.__and__)
@Connection.register
class Or(BinaryConnection):
"""
>>> Or.try_parse("123 OR b -> a")
Or(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
>>> Connection.parse("123 OR b -> a")
Or(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
"""
name = "or"
re_binary = re.compile(BinaryConnection.RE_BINARY_TEMPLATE.format("OR"))
apply_to_values = staticmethod(int.__or__)
@Connection.register
class LShift(BinaryConnection):
"""
>>> LShift.try_parse("123 LSHIFT b -> a")
LShift(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
>>> Connection.parse("123 LSHIFT b -> a")
LShift(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
"""
name = "lshift"
re_binary = re.compile(BinaryConnection.RE_BINARY_TEMPLATE.format("LSHIFT"))
apply_to_values = staticmethod(int.__lshift__)
@Connection.register
class RShift(BinaryConnection):
"""
>>> RShift.try_parse("123 RSHIFT b -> a")
RShift(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
>>> Connection.parse("123 RSHIFT b -> a")
RShift(lhs=Constant(value=123), rhs=Wire(target='b'),
destination=Wire(target='a'))
"""
name = "rshift"
re_binary = re.compile(BinaryConnection.RE_BINARY_TEMPLATE.format("RSHIFT"))
apply_to_values = staticmethod(int.__rshift__)
Challenge.main()
challenge = Challenge()