-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16_part1.py
executable file
·59 lines (52 loc) · 2.06 KB
/
day16_part1.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
#!/usr/bin/env python3
def parse_literal_pkt(remaining, version_sum):
while remaining[0] == "1":
remaining = remaining[5:]
remaining = remaining[5:]
return (remaining, version_sum)
def parse_operator_pkt(remaining, version_sum):
length_type_id = remaining[0]
remaining = remaining[1:]
if length_type_id == "1":
num_subpackets = int(remaining[:11],2)
remaining = remaining[11:]
remaining,version_sum = parse_subpackets_2(remaining, num_subpackets, version_sum)
else:
len_subpackets = int(remaining[:15],2)
remaining = remaining[15:]
remaining,version_sum = parse_subpackets_1(remaining, len_subpackets, version_sum)
return (remaining, version_sum)
def parse_subpackets_1(remaining, len_subpackets, version_sum):
i = 0
while i < len_subpackets:
version = remaining[:3]
version_sum += int(version,2)
type_id = remaining[3:6]
remaining = remaining[6:]
pre_length = len(remaining)
if type_id == '100': # literal value pkt
remaining,version_sum = parse_literal_pkt(remaining, version_sum)
else:
remaining,version_sum = parse_operator_pkt(remaining, version_sum)
i += (pre_length - len(remaining)) + 6
return (remaining, version_sum)
def parse_subpackets_2(remaining, num_subpackets, version_sum):
subpackets_visited = 0
while subpackets_visited < num_subpackets:
version = remaining[:3]
version_sum += int(version,2)
type_id = remaining[3:6]
remaining = remaining[6:]
if type_id == '100': # literal value pkt
remaining,version_sum = parse_literal_pkt(remaining, version_sum)
else:
remaining,version_sum = parse_operator_pkt(remaining, version_sum)
subpackets_visited += 1
return (remaining, version_sum)
with open("input.txt", "r") as f:
h = f.readline().rstrip()
b = bin(int(h, 16))[2:].zfill(len(h)*4)
version = b[:3]
version_sum = int(version,2)
remaining = b[6:]
print(parse_operator_pkt(remaining, version_sum)[1])