From bf16b19b1f6deffd1983efca059db576f3b60ee5 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 13 Dec 2021 10:40:39 +0300 Subject: 2019, tracking --- 2019/day6/orbits.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2019/day6/orbits.py (limited to '2019/day6/orbits.py') 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 @@ +# this solves part 2 +import sys + +import networkx as nx + + +def main(argv): + + G = nx.DiGraph() + with open(argv[0]) as fp: + for line in fp: + line = line.rstrip("\n") + start, end = line.split(")") + G.add_node(start, label=start) + G.add_node(end, label=end) + G.add_edge(start, end) + + jump_to = nx.lowest_common_ancestor(G, "SAN", "YOU") # SAN and YOU are given + jumps = ( + len(nx.shortest_path(G, jump_to, "SAN")) + + len(nx.shortest_path(G, jump_to, "YOU")) + - 4 + ) # 4 because len includes start and end, so -2 times 2 + print(jumps) + + +if __name__ == "__main__": + main(sys.argv[1:]) -- cgit v1.2.3-70-g09d2