Advertisement
Musical_Muze

Day 6, Part 2

Dec 9th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 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(")")    #orbits is a list of all orbits
  7.  
  8. list1 =[]
  9. for x in range(len(orbits)):
  10.     for y in range(len(orbits[x])):
  11.         list1.append(orbits[x][y])
  12. planets = list(set(list1))      #planets is a list of all planets, with no duplicates
  13.  
  14. for i in range(len(planets)-1):     #delete COM from planets
  15.     if (planets[i]=="COM"):
  16.         del(planets[i])
  17.  
  18. #recursive function to return an array of the orbits from a certain planet to COM
  19. def findOrbits(orbitArray, world, array1):
  20.     for i in orbitArray:
  21.  
  22.         localArray = array1
  23.         endCheck = i[0]
  24.         worldCheck = i[1]
  25.        
  26.         if(worldCheck==world):
  27.             if(endCheck!="COM"):
  28.                 localArray.append(i)
  29.                 returnArray = findOrbits(orbitArray,endCheck,localArray)
  30.             else:
  31.                 localArray.append(i)
  32.                 returnArray = localArray
  33.                 break
  34.        
  35.     return returnArray
  36.  
  37.  
  38. #find where the orbits intersect and return the count it took to get there
  39. def intersect(list1, list2):
  40.     count = 0
  41.     tempCount = 0
  42.     for x in list1:
  43.         for y in list2:
  44.             if(x[0]==y[0] and x!=y):
  45.                 count = list1.index(x) + list2.index(y)
  46.     return count
  47.  
  48. #get an array for the orbit lists of both YOU and SAN, going back to COM
  49. youList = []
  50. santaList = []
  51.  
  52. youList = findOrbits(orbits, "YOU", youList)
  53. santaList = findOrbits(orbits, "SAN", santaList)
  54.  
  55. #get the total count of changes of orbit from YOU to SAN
  56. print("The changes of orbit to get to Santa is: " + str(intersect(youList,santaList)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement