diff options
author | Yigit Sever | 2021-12-13 10:40:39 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-13 10:40:39 +0300 |
commit | bf16b19b1f6deffd1983efca059db576f3b60ee5 (patch) | |
tree | 1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day6/orbits.py | |
parent | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff) | |
download | aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2 aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip |
2019, tracking
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:]) | ||