2
1
Fork 0

Day 16: Add Python solution

This commit is contained in:
Mia Herkt 2021-12-17 01:13:09 +01:00
parent 0751db1e06
commit a775f8fcb1
Signed by: mia
GPG Key ID: 72E154B8622EC191
2 changed files with 58 additions and 0 deletions

1
16/input.txt Normal file
View File

@ -0,0 +1 @@
E20D4100AA9C0199CA6A3D9D6352294D47B3AC6A4335FBE3FDD251003873657600B46F8DC600AE80273CCD2D5028B6600AF802B2959524B727D8A8CC3CCEEF3497188C017A005466DAA6FDB3A96D5944C014C006865D5A7255D79926F5E69200A164C1A65E26C867DDE7D7E4794FE72F3100C0159A42952A7008A6A5C189BCD456442E4A0A46008580273ADB3AD1224E600ACD37E802200084C1083F1540010E8D105A371802D3B845A0090E4BD59DE0E52FFC659A5EBE99AC2B7004A3ECC7E58814492C4E2918023379DA96006EC0008545B84B1B00010F8E915E1E20087D3D0E577B1C9A4C93DD233E2ECF65265D800031D97C8ACCCDDE74A64BD4CC284E401444B05F802B3711695C65BCC010A004067D2E7C4208A803F23B139B9470D7333B71240050A20042236C6A834600C4568F5048801098B90B626B00155271573008A4C7A71662848821001093CB4A009C77874200FCE6E7391049EB509FE3E910421924D3006C40198BB11E2A8803B1AE2A4431007A15C6E8F26009E002A725A5292D294FED5500C7170038C00E602A8CC00D60259D008B140201DC00C401B05400E201608804D45003C00393600B94400970020C00F6002127128C0129CDC7B4F46C91A0084E7C6648DC000DC89D341B23B8D95C802D09453A0069263D8219DF680E339003032A6F30F126780002CC333005E8035400042635C578A8200DC198890AA46F394B29C4016A4960C70017D99D7E8AF309CC014FCFDFB0FE0DA490A6F9D490010567A3780549539ED49167BA47338FAAC1F3005255AEC01200043A3E46C84E200CC4E895114C011C0054A522592912C9C8FDE10005D8164026C70066C200C4618BD074401E8C90E23ACDFE5642700A6672D73F285644B237E8CCCCB77738A0801A3CFED364B823334C46303496C940

57
16/solution.py Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env python3
import sys
from operator import mul
from functools import reduce
from itertools import tee, takewhile, islice
packet = sys.stdin.readline().rstrip()
bitstr = "".join([f"{int(x,16):04b}" for x in packet])
ops = {
0: sum,
1: lambda l: reduce(mul, l, 1),
2: min,
3: max,
5: lambda l: 1 if l[0] > l[1] else 0,
6: lambda l: 1 if l[0] < l[1] else 0,
7: lambda l: 1 if l[0] == l[1] else 0,
}
def decode(p):
ver = int(p[0:3],2)
pid = int(p[3:6],2)
versum = ver
if pid == 4:
a, b = tee(map(lambda b: (int(b[0],2), int("".join(b[1:]), 2)),
zip(*(iter(p[6:]),)*5)))
n = sum(takewhile(lambda s: s, map(lambda x: x[0], a))) + 1
val = reduce(lambda a, b: a << 4 | b[1], islice(b, n), 0)
plen = 6 + n * 5
return plen, val, ver
else:
typ = int(p[6],2)
rvals = []
start = 7+(11 if typ else 15)
plen = int(p[7:start],2)
if typ:
for i in range(plen):
l, v, vn = decode(p[start:])
rvals.append(v)
start += l
versum+=vn
else:
while plen:
l, v, vn = decode(p[start:start+plen])
rvals.append(v)
start += l
plen -= l
versum+=vn
rval = ops[pid](rvals)
return start, rval, versum
_, gold, silver = decode(bitstr)
print(f"Silver: {silver}\nGold: {gold}")