-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.py
50 lines (35 loc) · 1015 Bytes
/
Day12.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
40
41
42
43
44
45
46
47
48
49
50
import json
with open("Day12input.txt") as f:
data = f.read()
# the first part by brute force
digitsPlus = "-0123456789"
filtered = list(map(lambda c: ' ' if c not in digitsPlus else c, data))
filteredStr = "".join(filtered)
items = filteredStr.split()
numbers = map(lambda x: int(x), items)
print(sum(numbers))
# part 2
def remRed(jd):
if isinstance(jd, dict):
for k, v in jd.items():
if k == "red" or v == "red":
return None
result = dict()
for k, v in jd.items():
result[k] = remRed(v)
return result
elif isinstance(jd, list):
result = []
for i in jd:
result.append(remRed(i))
return result
else:
return jd
jData = json.loads(data)
jClean = remRed(jData)
data = str(jClean)
filtered = list(map(lambda c: ' ' if c not in digitsPlus else c, data))
filteredStr = "".join(filtered)
items = filteredStr.split()
numbers = map(lambda x: int(x), items)
print(sum(numbers))