Advertisement
MyNamesNotReallyDave

AoC_2019_Day_3_Part_1a

Dec 9th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | None | 0 0
  1. #! python3
  2.  
  3. #link https://adventofcode.com/2019/day/3
  4.  
  5. #TODO: Figure out how to write to .csv instead of .txt!
  6. #! import csv
  7. from time import process_time #i For timing the step calculation process
  8.  
  9. def clean(wire): #i Split and strip the input to produce a clean list
  10.     wire = wire.split(',')
  11.     for x in range(len(wire)):
  12.         wire[x] = wire[x].strip()
  13.     return wire
  14.  
  15. #TODO: Figure out how to write to .csv instead of .txt!
  16. def save_list(name, in_list): #i Save to file function
  17.     filename = f'./Day_3/{name}.txt' #i Generate filename
  18.     with open(filename, 'w') as f: #i Context manager
  19.         f.write(str(in_list)) #i Write the list to file as a string
  20.  
  21. with open('./Day_3/Day_3_Input.txt', 'r') as f: #i Read the input into the two wires and apply the clean() function to them both
  22.     wire1, wire2 = f.readlines()
  23.     wire1, wire2 = [clean(w) for w in [wire1, wire2]]
  24.  
  25. #print(f'{"-"*30}\nWire 1: {wire1}\nWire 2: {wire2}') #i Print the 'clean' list of instructions for each wire
  26.  
  27. [save_list(n, s) for n, s in [['wire1_string', wire1], ['wire2_string', wire2]]] #i Save both wire strings to file
  28. print('Both wires saved to file...')
  29.  
  30. wire1_instructions = [] #i Instantiate empty lists to hold the co-ordinates
  31. wire2_instructions = []
  32.  
  33. def interpret(wire, instructions_list): #i Split the list items into tuples of direction and distance, inverting any distances in a negative direction
  34.     for instruction in wire:    #i For each instruction in the list of instructions (the wire)
  35.         direction = instruction[0] #i Direction is the letter (R = right, U = up etc.)
  36.         distance = int(instruction[1:]) #i Distance is the value (everything after the letter)
  37.  
  38.         if direction in ('L', 'D'): #i If the distance is negative (ie Left or Down)
  39.             distance *= -1 #i Invert the number
  40.  
  41.         instructions_list.append((direction, distance)) #i Append instruction tuples to the approperiate list
  42.  
  43. [interpret(w, c) for w, c in [[wire1, wire1_instructions], [wire2, wire2_instructions]]] #i Run the interpret() function on both wires, saving the output co-ords to the appropriate lists
  44.  
  45. #print(f'{"-"*30}\nWire 1 Instructions: {wire1_instructions}\nWire 2 Instructions: {wire2_instructions}') #i Print the lists of co-ordinates
  46.  
  47. [save_list(n, s) for n, s in [['wire1_instructions', wire1_instructions], ['wire2_instructions', wire2_instructions]]] #i Save both sets of instructions to file
  48. print('Both instruction sets saved...')
  49.  
  50. wire1_steps = [] #i Instantiate empty lists to hold the steps for each wire
  51. wire2_steps = []
  52.  
  53. def steps(wire, steps_list): #i Function to calculate and return the steps required to perform each instruction
  54.     if len(steps_list) == 0: #i If the steps_list is empty
  55.         steps_list.append((0,0)) #i Add the first step as the origin
  56.     for instruction in wire: #i For each instruction in the wire (list of instructions)
  57.         if instruction[0] in ('L', 'R'): #i If the direction is on the 'x' axis
  58.             axis = 0 #i Set the axis value to 0
  59.         else: #i If the direction is on the 'y' axis
  60.             axis = 1 #i Set the axis value to 1
  61.         if int(instruction[1]) >= 0: #i If the distance is positive
  62.             iterations = int(instruction[1]) + 1 #i Set the number of iterations to the value of the distance (+1 to account for starting at 0)
  63.         else: #i If the distance is negative
  64.             iterations = (int(instruction[1]) * -1) + 1 #i Set the number of iterations to the value of the distance (inverted to make positive) (+1 to account for starting at 0)
  65.         for i in range(1, iterations): #i For each of the determined iterations (distance of the instruction)
  66.             prev_step = steps_list[len(steps_list)-1] #i The previous step is the last one in the list
  67.             this_step = list(prev_step) #i This step is initially a copy of the previous step
  68.             if int(instruction[1]) == 0: #! If the distance is zero - probably not needed as zero distance = 0 iterations, which would be skipped
  69.                 this_step[axis] += 0 #! Don't change anything
  70.             elif int(instruction[1]) > 0: #i If the distance is positive
  71.                 this_step[axis] += 1 #i Increment the relevant axis by 1
  72.             else: #i If the distance is positive
  73.                 this_step[axis] -= 1 #i Reduce the relevant axis by 1
  74.             steps_list.append(tuple(this_step)) #i Add this step into the list of steps
  75.  
  76. print('Starting timer...')
  77.  
  78. t1_start = process_time() #i Start timing the step calulation process
  79.  
  80. [steps(c, s) for c, s in [[wire1_instructions, wire1_steps], [wire2_instructions, wire2_steps]]] #i Run the steps() function on each of the co-ordinate lists, saving the output to the appropriate steps lists
  81.  
  82. t1_stop = process_time() #i Stop timing the step calculation process
  83.  
  84. #print(f'{"-"*30}\nWire 1 Steps: {wire1_steps}\nWire 2 Steps: {wire2_steps}') #i Print the lists of steps for each wire
  85.  
  86. elapsed_time = t1_stop - t1_start #i Calculate the elapsed time
  87.  
  88. print(f'Time taken to compute both strings (total length {len(wire1_steps) + len(wire2_steps)}) was {elapsed_time}') #i Print the combined length and elapsed time
  89.  
  90. [save_list(n, s) for n, s in [['wire1_steps', wire1_steps], ['wire2_steps', wire2_steps]]] #i Save both sets of calculated steps to file
  91. print('Both step files saved...')
  92.  
  93. print('Program completed.') #i Notify user of program completion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement