33 lines
763 B
Python
33 lines
763 B
Python
|
with open("input.txt","r") as f:
|
||
|
lines = f.read().split('\n')
|
||
|
orbits = {}
|
||
|
for line in lines:
|
||
|
parent, child = line.split(')')
|
||
|
orbits[child] = parent
|
||
|
|
||
|
def depth(node, graph):
|
||
|
depth = 1
|
||
|
parent = graph[node]
|
||
|
while parent != "COM":
|
||
|
depth += 1
|
||
|
parent = graph[parent]
|
||
|
return depth
|
||
|
|
||
|
def parents(node, graph):
|
||
|
path = []
|
||
|
parent = graph[node]
|
||
|
while parent != "COM":
|
||
|
path.append(parent)
|
||
|
parent = graph[parent]
|
||
|
return path
|
||
|
|
||
|
def orbital_transfers(start, end, graph):
|
||
|
path1 = parents(start, graph)
|
||
|
path2 = parents(end, graph)
|
||
|
set1 = set(path1)
|
||
|
set2 = set(path2)
|
||
|
return len((set1 - set2) | (set2 - set1)) # union of differences
|
||
|
|
||
|
part1 = sum([depth(x, orbits) for x in orbits.keys()])
|
||
|
part2 = orbital_transfers("YOU", "SAN", orbits)
|
||
|
print(part1, part2)
|