Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #get the list of orbits into a two-dimensional array
- input1 = open("input1.txt","r")
- orbits = input1.read()
- orbits = orbits.split("\n")
- for i in range(len(orbits)):
- orbits[i] = orbits[i].split(")") #orbits is a list of all orbits
- list1 =[]
- for x in range(len(orbits)):
- for y in range(len(orbits[x])):
- list1.append(orbits[x][y])
- planets = list(set(list1)) #planets is a list of all planets, with no duplicates
- for i in range(len(planets)-1): #delete COM from planets
- if (planets[i]=="COM"):
- del(planets[i])
- #recursive function to return an array of the orbits from a certain planet to COM
- def findOrbits(orbitArray, world, array1):
- for i in orbitArray:
- localArray = array1
- endCheck = i[0]
- worldCheck = i[1]
- if(worldCheck==world):
- if(endCheck!="COM"):
- localArray.append(i)
- returnArray = findOrbits(orbitArray,endCheck,localArray)
- else:
- localArray.append(i)
- returnArray = localArray
- break
- return returnArray
- #find where the orbits intersect and return the count it took to get there
- def intersect(list1, list2):
- count = 0
- tempCount = 0
- for x in list1:
- for y in list2:
- if(x[0]==y[0] and x!=y):
- count = list1.index(x) + list2.index(y)
- return count
- #get an array for the orbit lists of both YOU and SAN, going back to COM
- youList = []
- santaList = []
- youList = findOrbits(orbits, "YOU", youList)
- santaList = findOrbits(orbits, "SAN", santaList)
- #get the total count of changes of orbit from YOU to SAN
- print("The changes of orbit to get to Santa is: " + str(intersect(youList,santaList)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement