Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- #link https://adventofcode.com/2019/day/3
- #TODO: Figure out how to write to .csv instead of .txt!
- #! import csv
- from time import process_time #i For timing the step calculation process
- def clean(wire): #i Split and strip the input to produce a clean list
- wire = wire.split(',')
- for x in range(len(wire)):
- wire[x] = wire[x].strip()
- return wire
- #TODO: Figure out how to write to .csv instead of .txt!
- def save_list(name, in_list): #i Save to file function
- filename = f'./Day_3/{name}.txt' #i Generate filename
- with open(filename, 'w') as f: #i Context manager
- f.write(str(in_list)) #i Write the list to file as a string
- 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
- wire1, wire2 = f.readlines()
- wire1, wire2 = [clean(w) for w in [wire1, wire2]]
- #print(f'{"-"*30}\nWire 1: {wire1}\nWire 2: {wire2}') #i Print the 'clean' list of instructions for each wire
- [save_list(n, s) for n, s in [['wire1_string', wire1], ['wire2_string', wire2]]] #i Save both wire strings to file
- print('Both wires saved to file...')
- wire1_instructions = [] #i Instantiate empty lists to hold the co-ordinates
- wire2_instructions = []
- def interpret(wire, instructions_list): #i Split the list items into tuples of direction and distance, inverting any distances in a negative direction
- for instruction in wire: #i For each instruction in the list of instructions (the wire)
- direction = instruction[0] #i Direction is the letter (R = right, U = up etc.)
- distance = int(instruction[1:]) #i Distance is the value (everything after the letter)
- if direction in ('L', 'D'): #i If the distance is negative (ie Left or Down)
- distance *= -1 #i Invert the number
- instructions_list.append((direction, distance)) #i Append instruction tuples to the approperiate list
- [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
- #print(f'{"-"*30}\nWire 1 Instructions: {wire1_instructions}\nWire 2 Instructions: {wire2_instructions}') #i Print the lists of co-ordinates
- [save_list(n, s) for n, s in [['wire1_instructions', wire1_instructions], ['wire2_instructions', wire2_instructions]]] #i Save both sets of instructions to file
- print('Both instruction sets saved...')
- wire1_steps = [] #i Instantiate empty lists to hold the steps for each wire
- wire2_steps = []
- def steps(wire, steps_list): #i Function to calculate and return the steps required to perform each instruction
- if len(steps_list) == 0: #i If the steps_list is empty
- steps_list.append((0,0)) #i Add the first step as the origin
- for instruction in wire: #i For each instruction in the wire (list of instructions)
- if instruction[0] in ('L', 'R'): #i If the direction is on the 'x' axis
- axis = 0 #i Set the axis value to 0
- else: #i If the direction is on the 'y' axis
- axis = 1 #i Set the axis value to 1
- if int(instruction[1]) >= 0: #i If the distance is positive
- iterations = int(instruction[1]) + 1 #i Set the number of iterations to the value of the distance (+1 to account for starting at 0)
- else: #i If the distance is negative
- 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)
- for i in range(1, iterations): #i For each of the determined iterations (distance of the instruction)
- prev_step = steps_list[len(steps_list)-1] #i The previous step is the last one in the list
- this_step = list(prev_step) #i This step is initially a copy of the previous step
- if int(instruction[1]) == 0: #! If the distance is zero - probably not needed as zero distance = 0 iterations, which would be skipped
- this_step[axis] += 0 #! Don't change anything
- elif int(instruction[1]) > 0: #i If the distance is positive
- this_step[axis] += 1 #i Increment the relevant axis by 1
- else: #i If the distance is positive
- this_step[axis] -= 1 #i Reduce the relevant axis by 1
- steps_list.append(tuple(this_step)) #i Add this step into the list of steps
- print('Starting timer...')
- t1_start = process_time() #i Start timing the step calulation process
- [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
- t1_stop = process_time() #i Stop timing the step calculation process
- #print(f'{"-"*30}\nWire 1 Steps: {wire1_steps}\nWire 2 Steps: {wire2_steps}') #i Print the lists of steps for each wire
- elapsed_time = t1_stop - t1_start #i Calculate the elapsed time
- 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
- [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
- print('Both step files saved...')
- print('Program completed.') #i Notify user of program completion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement