Solutions for day 12.

This commit is contained in:
Bert Peters
2015-12-12 10:47:59 +01:00
parent da7ece3fa8
commit 3b13925a0e
2 changed files with 24 additions and 0 deletions

1
day-12/input.txt Normal file

File diff suppressed because one or more lines are too long

23
day-12/solution.py Normal file
View File

@@ -0,0 +1,23 @@
import fileinput
import re
import json
def totals(obj):
if isinstance(obj, int):
return obj
if isinstance(obj, list):
return sum(totals(i) for i in obj)
if not isinstance(obj, dict) or "red" in obj.values():
return 0
return sum(totals(i) for i in obj.values())
fileData = ''.join(line for line in fileinput.input())
# Solve the first part by regex, no parsing needed.
pattern = re.compile(r"-?\d+")
total = sum(int(match) for match in pattern.findall(fileData))
data = json.loads(fileData)
print total, totals(data)