I way over thought this last night, and had a solution that worked but it was gnarly. Refactored this morning:
defformat_coords(coords):return[(val[0],int(val[1:]))forvalincoords.split(",")]DIRECTIONS={"R":(1,0),"L":(-1,0),"U":(0,1),"D":(0,-1)}deffind_points(paths):points={}x=0y=0steps=0fordirection,distanceinpaths:forpointinrange(distance):x_change,y_change=DIRECTIONS[direction]x+=x_changey+=y_changesteps+=1points[(x,y)]=stepsreturnpointsdefget_intersections(points1,points2):returnset(points1.keys()).intersection(set(points2.keys()))defget_manhattan_distances(points):return[abs(x)+abs(y)forx,yinpoints]defget_least_steps(intersections,points1,points2):return[points1[point]+points2[point]forpointinintersections]withopen("input.txt")as_file:paths1=format_coords(_file.readline())points1=find_points(paths1)paths2=format_coords(_file.readline())points2=find_points(paths2)intersections=get_intersections(points1,points2)# Part 1
print(min(get_manhattan_distances(intersections)))# Part 2
print(min(get_least_steps(intersections,points1,points2)))
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.
I way over thought this last night, and had a solution that worked but it was gnarly. Refactored this morning: