My Advent of Code solutions in Python.
kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code
python
1import fileinput
2import re
3import json
4
5def no_red_sum(x):
6 s = 0
7
8 if type(x) == list:
9 for y in x:
10 if type(y) == int:
11 s += y
12 else:
13 s += no_red_sum(y)
14 elif type(x) == dict:
15 for k in x.keys():
16 if x[k] == 'red':
17 return 0
18 s += no_red_sum(x[k])
19 elif type(x) == int:
20 return x
21
22 return s
23
24
25document = fileinput.input()[0]
26
27print "Sum of all numbers: %d" % sum(int(x) for x in re.findall('-?\d+', document))
28
29j = json.loads(document)
30
31print "Sum of non-reds items: %d" % no_red_sum(j)