diff options
Diffstat (limited to '2019/day6/orbits.py')
-rw-r--r-- | 2019/day6/orbits.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/2019/day6/orbits.py b/2019/day6/orbits.py new file mode 100644 index 0000000..652256f --- /dev/null +++ b/2019/day6/orbits.py | |||
@@ -0,0 +1,28 @@ | |||
1 | # this solves part 2 | ||
2 | import sys | ||
3 | |||
4 | import networkx as nx | ||
5 | |||
6 | |||
7 | def main(argv): | ||
8 | |||
9 | G = nx.DiGraph() | ||
10 | with open(argv[0]) as fp: | ||
11 | for line in fp: | ||
12 | line = line.rstrip("\n") | ||
13 | start, end = line.split(")") | ||
14 | G.add_node(start, label=start) | ||
15 | G.add_node(end, label=end) | ||
16 | G.add_edge(start, end) | ||
17 | |||
18 | jump_to = nx.lowest_common_ancestor(G, "SAN", "YOU") # SAN and YOU are given | ||
19 | jumps = ( | ||
20 | len(nx.shortest_path(G, jump_to, "SAN")) | ||
21 | + len(nx.shortest_path(G, jump_to, "YOU")) | ||
22 | - 4 | ||
23 | ) # 4 because len includes start and end, so -2 times 2 | ||
24 | print(jumps) | ||
25 | |||
26 | |||
27 | if __name__ == "__main__": | ||
28 | main(sys.argv[1:]) | ||