Skip to content
advent of code 2019

day 06

universal orbit map

approach

the orbit pairs become a dict from each body to the list of bodies orbiting it, a tree rooted at COM. part 1 is a recursive walk that adds each node's depth: the depth of a body is exactly how many direct and indirect orbits it has.

part 2 finds the path from COM down to YOU and the path down to SAN, as sets of nodes. the nodes the two paths share are the common ancestors; everything else has to be travelled, so the answer is the size of the symmetric difference.

the trick

the number of transfers is the number of nodes on one path but not the other.

python
def part2(o=orbits):
    d=construct(o)
    a=set(path_to(d,'COM','YOU',[]))
    b=set(path_to(d,'COM','SAN',[]))
    return len(a-b)+len(b-a)