-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2019day06.py
71 lines (57 loc) · 1.12 KB
/
2019day06.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# https://adventofcode.com/2019/day/6
import networkx as nx
def make_tree(s, directed=True):
if directed:
dg = nx.DiGraph()
else:
dg = nx.Graph()
for line in s.splitlines():
dst, src = line.strip().split(")")
dg.add_edge(src, dst)
return dg
def spath(test):
return [
max(v.values(), key=lambda i: len(i))
for (_, v) in list(nx.all_pairs_shortest_path(test))
]
def main():
test = make_tree(
"""COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L""".strip()
)
print(sum([len(i) - 1 for i in spath(test)]))
p2 = make_tree(
"""
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN""".strip(),
directed=False,
)
print(len(nx.shortest_path(p2, "YOU", "SAN")) - 3)
s = open("input").read().strip()
paths = spath(make_tree(s))
print(sum([len(i) - 1 for i in paths]))
p2 = make_tree(s, directed=False)
print(len(nx.shortest_path(p2, "YOU", "SAN")) - 3)
if __name__ == "__main__":
main()