diff options
author | Prefetch | 2022-12-31 22:21:39 +0100 |
---|---|---|
committer | Prefetch | 2022-12-31 22:21:39 +0100 |
commit | 68615a9ad2c942254135cffb00cf25a84a3b1356 (patch) | |
tree | 1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /12/main.py |
Initial commit
Diffstat (limited to '12/main.py')
-rwxr-xr-x | 12/main.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/12/main.py b/12/main.py new file mode 100755 index 0000000..0887e88 --- /dev/null +++ b/12/main.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +import json + + + +def solve_partn(partn, data): + # Primitive data types + if isinstance(data, int) or isinstance(data, float): + return data + elif isinstance(data, str): + return 0 + + # Composite data types + result = 0 + if isinstance(data, list): + for d in data: + result += solve_partn(partn, d) + elif isinstance(data, dict): + for d in data.values(): + # Part 2: skip all objects containing "red" as value + if partn == 2 and isinstance(d, str) and d == "red": + return 0 + else: + result += solve_partn(partn, d) + return result + + + +def main(): + # Read the JSON data from the input text file + with open("input.txt", "r") as f: + data = json.loads(f.read()) + + print("Part 1 solution:", solve_partn(1, data)) # 119433 for me + print("Part 2 solution:", solve_partn(2, data)) # 68466 for me + + + +if __name__ == "__main__": + main() |