2015/day12.py

35 lines
681 B
Python

import sys
import json
from pprint import pprint
import re
from typing import Any
doc = json.load(sys.stdin)
part1, part2 = 0, 0
def iter(x):
if type(x) is list:
return list, list(enumerate(x))
else:
return dict, list(x.items())
stack: Any = [(type(doc) is dict and "red" in doc.values(), doc)]
while stack:
red, x = stack.pop()
if type(x) is dict:
red = red or "red" in x.values()
for v in x.values():
stack.append((red, v))
elif type(x) is list:
for v in x:
stack.append((red, v))
elif type(x) is int:
part1 += x
if not red:
part2 += x
print(part1, part2)