From 6eff8af332c5bc0b77f4a5cf93a98cef54646250 Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 3 Dec 2023 02:43:10 +0100 Subject: [PATCH] day 12, tried with jq, faild, use online cheat --- d12/cmd.txt | 3 +++ d12/run.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 d12/cmd.txt create mode 100644 d12/run.py diff --git a/d12/cmd.txt b/d12/cmd.txt new file mode 100644 index 0000000..ea1a8c3 --- /dev/null +++ b/d12/cmd.txt @@ -0,0 +1,3 @@ + +jq '.' input | grep -o -E '\-?[0-9]+' | sum0 + diff --git a/d12/run.py b/d12/run.py new file mode 100644 index 0000000..e95eeae --- /dev/null +++ b/d12/run.py @@ -0,0 +1,23 @@ +import sys +import json + + +def sum_of_item(item, skip_red=False): + + if isinstance(item, list): + return sum(sum_of_item(i, skip_red) for i in item) + + if isinstance(item, dict): + if skip_red and 'red' in item.values(): + return 0 + return sum(sum_of_item(i, skip_red) for i in item.values()) + + if isinstance(item, str): + return 0 + + if isinstance(item, int): + return item + +abacus = json.load(sys.stdin) +print(sum_of_item(abacus)) +print(sum_of_item(abacus, skip_red=True))