Files
aocpy/2015/d12.py
2024-01-31 21:20:45 -05:00

28 lines
532 B
Python

import json
data = open(0).read().strip()
part_2 = False
def addup(d):
r = 0
if isinstance(d, list):
for e in d:
v = addup(e)
if v is not None:
r += v
elif isinstance(d, dict):
for e in d.values():
v = addup(e)
if v is None:
return 0
r += v
elif isinstance(d, str):
if part_2 and d == "red":
return None
else:
r += d
return r
data = json.loads(data)
print(addup(data))