-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc12.py
45 lines (33 loc) · 1 KB
/
aoc12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""JSAbacusFramework.io
Advent of Code 2015, day 12
Solution by Geir Arne Hjelle, 2016-12-10
"""
# Standard library imports
import json
import pathlib
import sys
def add_numbers(j_obj, ignore_red):
try:
return 0 + j_obj
except TypeError:
pass
try:
values = j_obj.values()
if ignore_red and "red" in values:
return 0
except AttributeError:
values = j_obj
return sum(add_numbers(v, ignore_red) for v in values if not v == values)
def main(args):
"""Solve the problem for all file paths"""
for file_path in [pathlib.Path(p) for p in args if not p.startswith("-")]:
solve(file_path)
def solve(file_path):
"""Solve the problem for one file path"""
print(f"\n{file_path}:")
with open(file_path, mode="r") as fid:
j_obj = json.load(fid)
print(f"The total sum is {add_numbers(j_obj, False)}")
print(f"The sum when ignoring red is {add_numbers(j_obj, True)}")
if __name__ == "__main__":
main(sys.argv[1:])