Advertisement
Musical_Muze

Day 6, Part 1

Dec 6th, 2019
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. #get the list of orbits into a two-dimensional array
  2. input1 = open("input1.txt","r")
  3. orbits = input1.read()
  4. orbits = orbits.split("\n")
  5. for i in range(len(orbits)):
  6.     orbits[i] = orbits[i].split(")")
  7.  
  8. list1 =[]
  9.  
  10. for x in range(len(orbits)):
  11.     for y in range(len(orbits[x])):
  12.         list1.append(orbits[x][y])
  13.  
  14. #planets is a list of all planets, with no duplicates
  15. planets = list(set(list1))
  16.  
  17. for i in range(len(planets)-1):     #delete COM from planets
  18.     if (planets[i]=="COM"):
  19.         del(planets[i])
  20.  
  21. #recursive function to find and count all direct and indirect orbits of a given planet,
  22. #going backwards
  23. def findOrbits(orbitArray, world, count):
  24.     for i in orbitArray:
  25.        
  26.         localCount = count
  27.         endCheck = i[0]
  28.         worldCheck = i[1]
  29.        
  30.         if(worldCheck==world):
  31.             if(endCheck!="COM"):
  32.                 localCount += 1
  33.                 returnValue = findOrbits(orbitArray,endCheck,localCount)
  34.             else:
  35.                 localCount +=1
  36.                 returnValue = localCount
  37.                
  38.     return returnValue
  39.  
  40. #total count of all direct and indirect orbits
  41. orbitCount = 0
  42.  
  43. #loop to find anc count all orbits of every planet
  44. for z in planets:
  45.     number = findOrbits(orbits, z, 0)
  46.     orbitCount += number
  47.  
  48. #print the resulting orbit count
  49. print("The total direct and indirect orbits is " + str(orbitCount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement