import json from typing import Any, Iterator, TypeAlias from ..base import BaseSolver JsonObject: TypeAlias = dict[str, "JsonObject"] | list["JsonObject"] | int | str def json_sum(value: JsonObject, ignore: str | None = None) -> int: if isinstance(value, str): return 0 elif isinstance(value, int): return value elif isinstance(value, list): return sum(json_sum(v, ignore=ignore) for v in value) elif ignore not in value.values(): return sum(json_sum(v, ignore=ignore) for v in value.values()) else: return 0 class Solver(BaseSolver): def solve(self, input: str) -> Iterator[Any]: data: JsonObject = json.loads(input) yield json_sum(data) yield json_sum(data, "red")