Advertisement
Musical_Muze

Day 3, Part 1

Dec 4th, 2019
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #load the wire input into arrays for processing
  2. input1 = open("wire1.txt", "r")
  3. input2 = open("wire2.txt", "r")
  4.  
  5. wire1 = input1.read()       #reads the file into a variable as one long string
  6. wire2 = input2.read()
  7.  
  8. wire1 = wire1.split(",")    #turns the string into an array of smaller strings
  9. wire2 = wire2.split(",")
  10.  
  11. input1.close()
  12. input2.close()
  13.  
  14. pos = [0,0]     #set initial position
  15.  
  16. def move(dir1):     #function for moving the position by one, given an input direction
  17.     if(dir1=="U"):
  18.         pos[1] += 1
  19.     elif(dir1=="D"):
  20.         pos[1] -= 1
  21.     elif(dir1=="L"):
  22.         pos[0] -= 1
  23.     elif(dir1=="R"):
  24.         pos[0] += 1
  25.     else:
  26.         print("unknown direction")
  27.  
  28. def toCenter(num1, num2):       #function for finding the distance from [0,0]
  29.     distance = abs(num1) + abs(num2)
  30.     return distance
  31.  
  32. #plot the first wire
  33. plot1 = open("plot1.txt", "w")
  34. for i in range(len(wire1)-1):     #one loop iteration per instruction
  35.     command = list(wire1[i])    
  36.     direction = command[0]      #first character is the direction
  37.     length = ""                 #remaining characters are the length
  38.     for i in range(1,len(command)):
  39.         length += command[i]
  40.     length = int(length)
  41.     for i in range(length):     #move position one at a time and log it
  42.         move(direction)
  43.         plot1.write(str(pos[0]) + "," + str(pos[1]) + "\n")
  44. plot1.close()
  45.  
  46. pos = [0,0]
  47.  
  48. #plot the second wire
  49. plot2 = open("plot2.txt", "w")
  50. for i in range(len(wire2)-1):    
  51.     command = list(wire2[i])    
  52.     direction = command[0]      
  53.     length = ""                
  54.     for i in range(1,len(command)):
  55.         length += command[i]
  56.     length = int(length)
  57.     for i in range(length):    
  58.         move(direction)
  59.         plot2.write(str(pos[0]) + "," + str(pos[1]) + "\n")
  60. plot2.close()
  61.  
  62. #load the resulting plots back into arrays to be processed
  63. check1 = open("plot1.txt", "r")
  64. check2 = open("plot2.txt", "r")
  65.  
  66. plot1 = check1.read()   #reuse the plot variables since they're no longer in use
  67. plot2 = check2.read()
  68.  
  69. plot1 = plot1.split("\n")
  70. plot2 = plot2.split("\n")
  71.  
  72. low = 10000000000       #a ridiculously high standard to find the lowest distance to (0,0)
  73.  
  74. #loop through both files and see how many positions are equal
  75. #these will be the intercepts
  76. for x in range(len(plot1)-2):
  77.     for y in range(len(plot2)-2):
  78.         if (plot1[x] == plot2[y]):  #if the positions are equal
  79.             print("The wires cross at position " + plot1[x])
  80.             cross = plot1[x].split(",")
  81.             dist = toCenter(int(cross[0]), int(cross[1]))   #get the distance and compare to the current low
  82.             if(dist < low):
  83.                 low = dist
  84.                 lowPos = plot1[x]
  85.  
  86. print("The closest intercept to the center is at " + lowPos + " at a distance of " + str(low))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement