40 lines
966 B
Python
40 lines
966 B
Python
#!/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]))
|