Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. lines = list(open('input.txt').readlines())
  2.  
  3. parentsDict = {}
  4. for line in lines:
  5. par, kid = line[:-1].split(')')
  6. parentsDict[kid] = par
  7.  
  8. def ancestors(kid):
  9. if kid not in parentsDict:
  10. return [] # base case: COM orbits no one
  11. par = parentsDict[kid]
  12. return [par] + ancestors(par)
  13.  
  14. def main1():
  15. print(sum(len(ancestors(kid)) for kid in parentsDict))
  16.  
  17. def main2():
  18. x, y = ['SAN', 'YOU']
  19. [a_x, a_y] = map(ancestors, [x, y])
  20. print(len(set(a_x) ^ set(a_y)))
  21.  
  22. main1()
  23. main2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement