-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp16a.py
77 lines (66 loc) · 1.55 KB
/
p16a.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
# https://adventofcode.com/2021/day/16 part 1
# Created by: Menaka S. 16 Dec 2021
import sys
import binascii
line = sys.stdin.read()
line = line.strip()
ind = 0
vtotal = 0
def hextobin(word):
scale = 16 ## equals to hexadecimal
num_of_bits = 16
return bin(int(word, scale))[2:].zfill(num_of_bits)
def binaryToDecimal(binary):
binary1 = binary
decimal, ind, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, ind)
binary = binary//10
ind += 1
return decimal
def decode():
global vtotal
global ind
if ind >= len(inp):
return
pversion = inp[ind:ind+3]
vtotal += binaryToDecimal(int(pversion))
ptypeid = inp[ind+3:ind+6]
plengthtype = inp[ind+6]
#print("\n....Decoding",ind,pversion,ptypeid,plengthtype,vtotal)
if ptypeid != '100': #operator
ind += 7
#print("Operator")
if plengthtype == '0':
length = binaryToDecimal(int(inp[ind:ind+15]))
#print("==Length of bits",inp[ind:ind+15],length)
ind +=15
length = ind+length
while ind <length:
decode()
ind = length
else:
num = binaryToDecimal(int(inp[ind:ind+11]))
#print("==Num of subpackets",num)
ind += 11
for j in range(num):
decode()
else: #literal
ind += 6
numstr = ''
#print("Literal",inp[ind:])
while inp[ind] == '1':
numstr = numstr + inp[ind+1:ind+5]
ind += 5
numstr += inp[ind+1:ind+5]
num = binaryToDecimal(int(numstr))
#print(numstr,num)
ind += 5
#print(line)
inp = hextobin(line)
while len(inp) %4:
inp = '0' + inp
#print(inp)
decode()
print(vtotal)