summaryrefslogtreecommitdiffstats
path: root/2019/day6/orbits.py
blob: 652256fd4154c0d3e23a8097c542fac071357ece (plain)
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
# 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:])