-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday07.py
37 lines (31 loc) · 802 Bytes
/
day07.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
with open("input.txt") as f:
s = f.read().strip().split("\n")
weights = {}
children = {}
seen = set()
for p in s:
pn = p[:p.find(" ")]
pw = int(p[p.find("(")+1:p.find(")")])
weights[pn] = pw
if "->" in p:
children[pn] = p[p.find("->")+3:].split(", ")
seen.update(children[pn])
root = None
for p in weights:
if p not in seen:
root = p
print(root)
def find(c):
w = weights[c]
if c in children:
cw = [find(n) for n in children[c]]
if -1 in cw:
return -1
w += sum(cw)
if len(set(cw)) != 1:
for i in range(len(cw)):
if cw[i] != cw[i-1] and cw[i] != cw[i-2]:
print(weights[children[c][i]] - cw[i] + cw[i-1])
return -1
return w
find(root)