-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.py
52 lines (37 loc) · 1006 Bytes
/
07.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
51
52
from lib import *
input = read_input(2017, 7)
lines = input.splitlines()
names = set()
subs = set()
for line in lines:
name = line.split()[0]
sub = [] if "->" not in line else line.split("-> ")[1].split(", ")
names.add(name)
subs.update(sub)
print(next(iter(names - subs)))
progs = {}
names = set()
subs = set()
for line in lines:
name = line.split()[0]
weight = int(line.split("(")[1].split(")")[0])
sub = [] if "->" not in line else line.split("-> ")[1].split(", ")
progs[name] = weight, sub
names.add(name)
subs.update(sub)
root = next(iter(names - subs))
def check_sum(p):
s = progs[p][0]
w = {}
for q in progs[p][1]:
a, b = check_sum(q)
if a is not None:
return a, None
s += b
w[q] = b
x = max(w.values(), key=list(w.values()).count, default=None)
for k, v in w.items():
if v != x:
return x - (v - progs[k][0]), None
return None, s
print(check_sum(root)[0])