-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.1.py
207 lines (181 loc) · 5.4 KB
/
day16.1.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
import sys
from io import StringIO
from functools import partial
def test_lval():
fd = StringIO("D2FE28")
bs = BitStream(fd)
root = parse(bs)
assert bs.bits == 21
bs.flush()
assert bs.bits == 24
assert root.lval == 2021
def test_op_len0():
fd = StringIO("38006F45291200")
bs = BitStream(fd)
root = parse(bs)
assert len(root.pkts) == 2
assert root.lenval == 27
vals = (
(6, 4, 10),
(2, 4, 20),
)
for i, p in enumerate(root.pkts):
x, y, z = vals[i]
# print('i', i)
# print('p', p)
assert p.ver == x
assert p.tid == y
assert p.lval == z
def test_op_len1():
fd = StringIO("EE00D40C823060")
bs = BitStream(fd)
root = parse(bs)
assert len(root.pkts) == 3
assert root.lenval == 3
vals = (1, 2, 3)
for i, p in enumerate(root.pkts):
assert p.lval == vals[i]
def read_num_bits(chrstrm, n):
"""Read `n` no. of bits from `chrstrm`"""
bits = ""
for i in range(n):
ch = next(chrstrm)
bits += ch
return bits
class BitStream:
def __init__(self, fd):
self.fd = fd
self.currword = ""
self.bitsleft = ""
self.stream = self._bit_gen()
self.bits = 0
def __iter__(self):
return self
def __next__(self):
self._inc_bit()
return next(self.stream)
def _inc_bit(self):
self.bits += 1
def _word_gen(self):
"""Read 8 bits at a time"""
b = self.fd.read(2)
while b.strip() != '':
self.currword = b
yield format(int(b, 16), '08b')
b = self.fd.read(2)
def _bit_gen(self):
"""Read a single bit from `word_gen` at a time"""
for w in self._word_gen():
for i, c in enumerate(w):
self.bitsleft = w[i+1:]
yield c
def flush(self):
"""Drain the buffer, move on to the next word"""
return [(next(self.stream), self._inc_bit()) for i in range(len(self.bitsleft))]
class Packet:
def __init__(self, ver, tid):
self.ver = int(ver)
self.tid = int(tid)
class LPacket(Packet):
def __init__(self, ver, tid, lval, pkt_len):
super().__init__(ver, tid)
self.lval = lval
self.pkt_len = pkt_len
def __str__(self):
return (f'<LPacket ver={self.ver}, tid={self.tid}, '
f'lval={self.lval}, pkt_len={self.pkt_len}>')
def set_val(self, lval):
self.lval = lval
class OPacket(Packet):
def __init__(self, ver, tid, lid, lenval):
super().__init__(ver, tid)
self.pkts = []
self.lid = lid
self.lenval = lenval
def __str__(self):
return (f'<OPacket pver={self.ver}, ptid={self.tid}, '
f'lenval={self.lenval} lid={self.lid} pkts={len(self.pkts)}>')
def add(self, pkt):
self.pkts.append(pkt)
getvtid = lambda x: (int(x[:3], 2), int(x[3:], 2))
def parse_lval(ver, tid, bs):
is_last = False
buff = ""
buff_len = 0
hdr_len = 6
while not is_last:
buff_len += 5
bits = read_num_bits(bs, 5)
is_last = bits[0] == '0'
buff += bits[1:]
pkt_len = hdr_len+buff_len
return LPacket(ver, tid, int(buff, 2), pkt_len)
def parse_operator(ver, tid, bs):
lid = int(read_num_bits(bs, 1))
lenfunc = [
lambda x: int(read_num_bits(x, 15), 2), # nob
lambda x: int(read_num_bits(x, 11), 2), # nop
][lid]
lenval = lenfunc(bs)
return OPacket(ver, tid, lid, int(lenval))
def parse(bs):
root = None
buff = ""
for bit in bs:
buff += bit
if len(buff) == 6:
ver, tid = getvtid(buff)
if int(tid) == 4:
# print("lval")
lpkt = parse_lval(ver, tid, bs)
return lpkt
else:
# print("not lval", ver, tid, buff)
opkt = parse_operator(ver, tid, bs)
# print(opkt)
if opkt.lid == 0:
lenval = opkt.lenval
currbits = bs.bits
# print('prev:', lenval)
# @TODO: try lenval > (bs.bits - currbits)
# while lenval - (bs.bits - currbits) > 0:
while lenval > (bs.bits - currbits):
subp = parse(bs)
opkt.add(subp)
return opkt
else:
lenval = opkt.lenval
pkt_cnt = 0
while lenval > pkt_cnt:
subp = parse(bs)
pkt_cnt += 1
opkt.add(subp)
return opkt
return opkt, bits
return root
def sum_vers(root):
if isinstance(root, LPacket):
return root.ver
ver = root.ver
for p in root.pkts:
ver += sum_vers(p)
return ver
def main(fd):
bs = BitStream(fd)
root = parse(bs)
print(sum_vers(root))
if __name__ == '__main__':
try:
fpath = sys.argv[1]
except IndexError:
print(f"python {sys.argv[0]} <input_file.txt>")
else:
with open(fpath) as f:
main(f)
# test_lval()
# test_op_len0()
# test_op_len1()
# # root = parse(BitStream(StringIO("8A004A801A8002F478")))
# root = parse(BitStream(StringIO("A0016C880162017C3686B18A3D4780")))
# # __import__('pdb').set_trace()
# print(sum_vers(root))