Day 16
This commit is contained in:
39
day16/part2.py
Normal file
39
day16/part2.py
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
from functools import reduce
|
||||
from operator import mul
|
||||
|
||||
from day16.shared import parse_packets
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
|
||||
|
||||
def calc(expr):
|
||||
_, op, value = expr
|
||||
if op == 4:
|
||||
return value
|
||||
values = [calc(v) if isinstance(v, tuple) else v for v in value]
|
||||
if op == 0:
|
||||
return sum(values)
|
||||
elif op == 1:
|
||||
return reduce(mul, values)
|
||||
elif op == 2:
|
||||
return min(values)
|
||||
elif op == 3:
|
||||
return max(values)
|
||||
elif op == 4:
|
||||
return values
|
||||
elif op == 5:
|
||||
return 1 if values[0] > values[1] else 0
|
||||
elif op == 6:
|
||||
return 1 if values[0] < values[1] else 0
|
||||
elif op == 7:
|
||||
return 1 if values[0] == values[1] else 0
|
||||
|
||||
|
||||
for line in lines:
|
||||
print("NEW LINE:", line)
|
||||
if line[0] == '#':
|
||||
continue
|
||||
bin_string = ''.join(bin(int(c, 16))[2:].zfill(4) for c in line)
|
||||
parsed = parse_packets(bin_string)
|
||||
print(calc(parsed[0]))
|
||||
Reference in New Issue
Block a user