-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc16.py
executable file
·175 lines (130 loc) · 3.94 KB
/
aoc16.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
#!/usr/bin/env python3
import functools
import operator
import utils
class BITSStream:
def __init__(self, hex_data):
if len(hex_data) % 2 != 0:
hex_data += "0"
self.data = bytes.fromhex(hex_data)
self.pos = 0
def next_bits(self, n):
result = 0
while n > 0:
src_byte = self.pos // 8
src_bit = self.pos % 8
read_bits = min(n, 8 - src_bit)
mask = (1 << read_bits) - 1
src = self.data[src_byte]
data = (src >> (8 - (src_bit + read_bits))) & mask
result |= data
n -= read_bits
self.pos += read_bits
result <<= n
return result
class PacketBase:
def __init__(self, version):
self.version = version
def print_header(self, indent):
print(" " * indent, end="")
print(f"{type(self).__name__} @ {self.version}: ", end="")
class Literal(PacketBase):
def __init__(self, version, _value):
super().__init__(version)
self._value = _value
def pretty_print(self, indent=0):
self.print_header(indent)
print(self._value)
def version_sum(self):
return self.version
def value(self):
return self._value
class Operator(PacketBase):
def __init__(self, version, packets):
super().__init__(version)
self.packets = packets
def pretty_print(self, indent=0):
self.print_header(indent)
print()
for packet in self.packets:
packet.pretty_print(indent + 1)
def version_sum(self):
return self.version + sum(p.version_sum() for p in self.packets)
def values(self):
return (p.value() for p in self.packets)
class Sum(Operator):
def value(self):
return sum(self.values())
class Product(Operator):
def value(self):
return functools.reduce(operator.mul, self.values(), 1)
class Minimum(Operator):
def value(self):
return min(self.values())
class Maximum(Operator):
def value(self):
return max(self.values())
class BinaryOperator(Operator):
def __init__(self, version, packets):
if len(packets) != 2:
raise ValueError(f"{type(self)} requires 2 packets, got {packets}")
super().__init__(version, packets)
class Gt(BinaryOperator):
def value(self):
if self.packets[0].value() > self.packets[1].value():
return 1
else:
return 0
class Lt(BinaryOperator):
def value(self):
if self.packets[0].value() < self.packets[1].value():
return 1
else:
return 0
class Eq(BinaryOperator):
def value(self):
if self.packets[0].value() == self.packets[1].value():
return 1
else:
return 0
OPERATORS = {
0: Sum,
1: Product,
2: Minimum,
3: Maximum,
5: Gt,
6: Lt,
7: Eq,
}
def read_literal_value(stream):
value = 0
while True:
next_group = stream.next_bits(5)
value <<= 4
value |= next_group & 0b1111
if next_group & 0b10000 == 0:
return value
def read_packet(stream):
version = stream.next_bits(3)
type_id = stream.next_bits(3)
if type_id == 4:
return Literal(version, read_literal_value(stream))
else:
length_type = stream.next_bits(1)
if length_type == 0:
packets = []
subpacket_length = stream.next_bits(15)
end = stream.pos + subpacket_length
while stream.pos < end:
packets.append(read_packet(stream))
else:
subpacket_count = stream.next_bits(11)
packets = [read_packet(stream) for _ in range(subpacket_count)]
return OPERATORS[type_id](version, packets)
def main():
stream = BITSStream(utils.test_input().read().strip())
packet = read_packet(stream)
packet.pretty_print()
print(packet.value())
if __name__ == "__main__":
main()