-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
39 lines (32 loc) · 834 Bytes
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
import re
import json
import pprint
def reject_red(obj):
match obj:
case list():
l = []
for o in obj:
l.append(reject_red(o))
return l
case dict():
if 'red' in obj.values():
return {}
else:
for k, v in obj.items():
obj[k] = reject_red(v)
return obj
case _:
return obj
def sum_numbers(string):
numbers = re.findall(r'-?\d+', string)
numbers = [int(n) for n in numbers]
return sum(numbers)
if __name__ == '__main__':
inputs = [line.rstrip('\n') for line in open(sys.argv[1])]
# p1
print(sum_numbers(inputs[0]))
# p2
j = json.loads(inputs[0])
j = reject_red(j)
print(sum_numbers(json.dumps(j)))