Advertisement
Musical_Muze

Day 3, Part 2

Dec 4th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #requires an intersections.txt file generated from my code in Day 3, Part 1
  2. #and both wire files from Part 1
  3.  
  4. #load the inputs into arrays
  5. file1 = open("intersections.txt", "r")
  6. input1 = open("wire1.txt", "r")
  7. input2 = open("wire2.txt", "r")
  8.  
  9. intersections = file1.read()
  10. wire1 = input1.read()       #reads the file into a variable as one long string
  11. wire2 = input2.read()
  12.  
  13. intersections = intersections.split("\n")
  14. wire1 = wire1.split(",")    #turns the string into an array of smaller strings
  15. wire2 = wire2.split(",")
  16.  
  17. file1.close()
  18. input1.close()
  19. input2.close()
  20.  
  21. shortStep = 100000000000000
  22. shortPos = ""
  23.  
  24. def move(dir1):     #function for moving the position by one, given an input direction
  25.     if(dir1=="U"):
  26.         pos[1] += 1
  27.     elif(dir1=="D"):
  28.         pos[1] -= 1
  29.     elif(dir1=="L"):
  30.         pos[0] -= 1
  31.     elif(dir1=="R"):
  32.         pos[0] += 1
  33.  
  34. def toString(array1):       #function to turn the position into a format that can be compared to the intersection
  35.     value = str(array1[0]) + "," + str(array1[1])
  36.     return value
  37.  
  38. #for each intersection, run the plot for wire1 and keep running total
  39. #until it hits the intersection, then run the same thing for wire2.
  40. for a in intersections:     #for each intersection
  41.     pos = [0,0]
  42.     stepCount = 0
  43.    
  44.     for b in range(len(wire1)-1):       #for each instruction on wire1
  45.        
  46.         command = list(wire1[b])    
  47.         direction = command[0]      #first character is the direction
  48.         length = ""                 #remaining characters are the length
  49.         for c in range(1,len(command)):
  50.             length += command[c]
  51.         length = int(length)
  52.        
  53.         for d in range(length):     #move position one at a time and check
  54.             move(direction)
  55.             stepCount += 1
  56.            
  57.             if(a == toString(pos)):       #once the position is reached,
  58.                 pos = [0,0]                         #reset position and run for wire2
  59.                                                     #keep stepCount
  60.                 for e in range(len(wire2)-1):    
  61.                     command = list(wire2[e])    
  62.                     direction = command[0]      
  63.                     length = ""                
  64.                     for f in range(1,len(command)):
  65.                         length += command[f]
  66.                     length = int(length)
  67.                    
  68.                     for g in range(length):    
  69.                         move(direction)
  70.                         stepCount += 1
  71.                        
  72.                         if(a == toString(pos)):
  73.                             print("The intersection " + a + " is " + str(stepCount) + " steps away.")
  74.                             if(stepCount<shortStep):
  75.                                 shortStep = stepCount
  76.                                 shortPos = a
  77.  
  78. print("The shortest step distance is " + str(shortStep) + " to intersection " + shortPos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement