Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def find_graph() :
- graphs = defaultdict(int)
- planets = set()
- with open('input.txt') as file :
- for line in file :
- stuff = line.strip().split(")")
- x, y = stuff[0], stuff[1] #y orbits x
- graphs[(x,y)] = 1
- planets.add(y)
- return graphs, planets
- def find_depth(tree, node) :
- try :
- parent = next(key for key, value in tree.items() if key[1] == node and value == 1)[0]
- return 1 + find_depth(tree, parent)
- except :
- return 0
- graphs, planets = find_graph()
- def findSum(graph=graphs, planet=planets) :
- sum=0
- for node in planet :
- sum += find_depth(graph, node)
- print(sum)
- #findSum()
- def findTransfers(tree=graphs, node1='YOU', node2='SAN') : #Find distance between 2 nodes on a tree
- #find ancestors of nodes
- tempNode = node1
- ancestors1 = []
- ancestors2 = []
- while True :
- try :
- parent = next(key for key, value in tree.items() if key[1] == tempNode and value == 1)[0]
- ancestors1.append(parent)
- tempNode = parent
- except :
- break
- tempNode = node2
- while True :
- try :
- parent = next(key for key, value in tree.items() if key[1] == tempNode and value == 1)[0]
- ancestors2.append(parent)
- tempNode = parent
- except :
- break
- for node in ancestors1 :
- if node in ancestors2 :
- common = node
- break
- print(common)
- length = ancestors1.index(common) + ancestors2.index(common) - 1
- print(length)
- findTransfers()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement