-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_16.py
213 lines (162 loc) · 6.19 KB
/
problem_16.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
import json
import dataclasses
from dataclasses import dataclass
# import struct
import numpy as np
data_path = "data/problem_16.txt"
# data_path = "data/problem_16_test.txt"
with open(data_path, "r") as f:
data = f.read().strip()
def hex_to_binary(hex_string):
return np.unpackbits(np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8))
def bits_to_string(bit_array):
return "".join([str(x) for x in bit_array])
@dataclass
class Packet:
version: int = None
@staticmethod
def _binary_to_int(bits):
exponents = np.arange(len(bits) - 1, -1, -1)
return np.bitwise_or.reduce(bits.astype(np.int64) << exponents)
@classmethod
def unpack(cls, bit_array, start=0):
"""Returns tuple of [packet, bits read]"""
# print(bits_to_string(bit_array[start:]))
offset = start
version = cls._binary_to_int(bit_array[offset:offset + 3])
offset += 3
type_id = cls._binary_to_int(bit_array[offset:offset + 3])
offset += 3
packet, bits_read = PACKET_TYPE_MAP[type_id].unpack(version, bit_array, offset)
offset += bits_read
if start == 0:
return packet
else:
return packet, offset - start
def get_version_sum(self):
if isinstance(self, OperatorPacket):
return self.version + sum((subpacket.get_version_sum() for subpacket in self.subpackets))
else:
return self.version
def print_tree(self, indent_level=0):
string = " " * (indent_level * 2) + "- "
if isinstance(self, LiteralPacket):
string += str(self.value)
else:
assert isinstance(self, OperatorPacket)
string += self.__class__.__name__.replace("Packet", "") + " = " + str(self.evaluate())
for subpacket in self.subpackets:
# subpacket_string = subpacket.get_string(indent_level=indent_level + 1)
string += "\n" + subpacket.print_tree(indent_level=indent_level + 1)
if indent_level == 0:
print(string)
else:
return string
@dataclass
class LiteralPacket(Packet):
value: int = None
@classmethod
def _unpack_literal_value(cls, bit_array, start):
chunk_count = np.argmin(bit_array[start::5]) + 1 # get index of first 5-bit chunk starting with a 0
literal_length = chunk_count * 5
# magically remove every 5th bit since that's not part of the number
filtered_bits = np.delete(bit_array[start:start + literal_length], slice(None, None, 5))
return cls._binary_to_int(filtered_bits), literal_length
@classmethod
def unpack(cls, version, bit_array, start):
literal_value, bits_read = cls._unpack_literal_value(bit_array, start)
return cls(version=version, value=literal_value), bits_read
def evaluate(self):
return self.value
@dataclass
class OperatorPacket(Packet):
subpackets: list = None
def evaluate_subpackets(self):
return [s.evaluate() for s in self.subpackets]
@classmethod
def _parse_subpacket_length(cls, bit_array, offset):
length_type = bit_array[offset]
length_size = 15 if length_type == 0 else 11
length = cls._binary_to_int(bit_array[offset + 1:offset + length_size + 1])
return length_type, length, length_size + 1
@classmethod
def unpack(cls, version, bit_array, start):
offset = start
subpacket_length_type, subpacket_length, bits_read = cls._parse_subpacket_length(bit_array, offset)
offset += bits_read
subpackets = []
if subpacket_length_type == 0:
subpacket_offset = 0
while subpacket_offset < subpacket_length:
subpacket, bits_read = Packet.unpack(bit_array, start=offset + subpacket_offset)
subpackets.append(subpacket)
subpacket_offset += bits_read
offset += subpacket_offset
else:
for i in range(subpacket_length):
subpacket, bits_read = Packet.unpack(bit_array, start=offset)
subpackets.append(subpacket)
offset += bits_read
packet = cls(version=version, subpackets=subpackets)
return packet, offset - start
@dataclass
class SumPacket(OperatorPacket):
def evaluate(self):
return np.sum(self.evaluate_subpackets())
@dataclass
class ProductPacket(OperatorPacket):
def evaluate(self):
return np.prod(self.evaluate_subpackets())
@dataclass
class MinimumPacket(OperatorPacket):
def evaluate(self):
return np.min(self.evaluate_subpackets())
@dataclass
class MaximumPacket(OperatorPacket):
def evaluate(self):
return np.max(self.evaluate_subpackets())
@dataclass
class GreaterThanPacket(OperatorPacket):
def evaluate(self):
assert len(self.subpackets) == 2
return np.int64(self.subpackets[0].evaluate() > self.subpackets[1].evaluate())
@dataclass
class LessThanPacket(OperatorPacket):
def evaluate(self):
assert len(self.subpackets) == 2
return np.int64(self.subpackets[0].evaluate() < self.subpackets[1].evaluate())
@dataclass
class EqualToPacket(OperatorPacket):
def evaluate(self):
assert len(self.subpackets) == 2
return np.int64(self.subpackets[0].evaluate() == self.subpackets[1].evaluate())
PACKET_TYPE_MAP = (
SumPacket, # 0
ProductPacket, # 1
MinimumPacket, # 2
MaximumPacket, # 3
LiteralPacket, # 4
GreaterThanPacket, # 5
LessThanPacket, # 6
EqualToPacket, # 7
)
# part 1
top_level_packet = Packet.unpack(hex_to_binary(data))
# top_level_packet = Packet.unpack(hex_to_binary("04005AC33890"))
# top_level_packet.print_tree()
print(f"Part 1 solution: {top_level_packet.get_version_sum()}")
# part 2
test_cases = (
("C200B40A82", 3),
("04005AC33890", 54),
("880086C3E88112", 7),
("CE00C43D881120", 9),
("D8005AC2A8F0", 1),
("F600BC2D8F", 0),
("9C005AC2F8F0", 0),
("9C0141080250320F1802104A08", 1),
)
for test_case, expected_value in test_cases:
test_packet = Packet.unpack(hex_to_binary(test_case))
assert test_packet.evaluate() == expected_value
print(f"Part 2 solution: {top_level_packet.evaluate()}")