Not sure if it's the most efficient way of doing it, but this is how I solved it
# Get all the directions from the input
frominputimportmoving_input_1,moving_input_2# Create an object with coordinates for each step
defpopulate_grid(moving_input):coordinates_all={}# Set the current position
x=0y=0# Counter for each time the position moves
step=0# Loop through all the directions given by the file
formoveinmoving_input:direction=move[0]distance=int(move[1:])# Pick in which direction to move
move_x=move_y=0ifdirection=="L":move_x=-1ifdirection=="R":move_x=1ifdirection=="D":move_y=-1ifdirection=="U":move_y=1# Do the actual movement
for_inrange(0,distance):x+=move_xy+=move_ystep+=1if(x,y)notincoordinates_all:coordinates_all[(x,y)]=stepreturncoordinates_all# Get the coordinate grids for each line
line_1=populate_grid(moving_input_1)line_2=populate_grid(moving_input_2)# Get the intersections for both lines
intersections=list(set(line_1.keys())&set(line_2.keys()))# Get the shortest distance
defget_shortest():distances=[]forintersectioninintersections:dist=abs(intersection[0])+abs(intersection[1])distances.append(dist)returnmin(distances)# Get the fewest steps
defget_fewest():combined_steps=[line_1[i]+line_2[i]foriinintersections]returnmin(combined_steps)print("SOLUTION",get_shortest(),get_fewest())
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Not sure if it's the most efficient way of doing it, but this is how I solved it