2015: d12: ex2: add solution
This commit is contained in:
parent
8ac7dfe5a7
commit
3c69c64c18
1 changed files with 36 additions and 0 deletions
36
2015/d12/ex2/ex2.py
Executable file
36
2015/d12/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from collections.abc import Iterator
|
||||||
|
|
||||||
|
JSONValue = int | str | list["JSONValue"] | dict[str, "JSONValue"]
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: str) -> int:
|
||||||
|
def parse(input: str) -> JSONValue:
|
||||||
|
return json.loads(input)
|
||||||
|
|
||||||
|
def all_numbers(doc: JSONValue) -> Iterator[int]:
|
||||||
|
if isinstance(doc, int):
|
||||||
|
yield doc
|
||||||
|
elif isinstance(doc, list):
|
||||||
|
for it in doc:
|
||||||
|
yield from all_numbers(it)
|
||||||
|
elif isinstance(doc, dict):
|
||||||
|
if "red" in doc.values():
|
||||||
|
return
|
||||||
|
for it in doc.values():
|
||||||
|
yield from all_numbers(it)
|
||||||
|
|
||||||
|
doc = parse(input)
|
||||||
|
return sum(all_numbers(doc))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = sys.stdin.read()
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue