day 12, tried with jq, faild, use online cheat

This commit is contained in:
setop 2023-12-03 02:43:10 +01:00
parent 067decada0
commit 6eff8af332
2 changed files with 26 additions and 0 deletions

3
d12/cmd.txt Normal file
View File

@ -0,0 +1,3 @@
jq '.' input | grep -o -E '\-?[0-9]+' | sum0

23
d12/run.py Normal file
View File

@ -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))