I'm using AoC this year to learn Python. Last year I used it to learn nodejs.
Day 3 was a doozy for me, partly because I started it at 11pm (I'm on American Central Time), but partly because I overthought the problem and thought I would map the whole thing as a 2D array. After screwing my head on a little bit more, I went with simply tracking the path of the wire with x/y tuples, then finding the intersections between the two wires. Manhattan distance was baked into each tuple (just add the absolute values of x and y together and voila).
For my first crack at Part One, I originally used sets. When I discovered Part Two, I just had to change the sets to lists and then I got path distance for free.
defmove(direction,coord,path):x=coord['x']y=coord['y']# remove any \n if it exists
dir=direction.rstrip()amt=int(dir[1:len(dir)])ifdir.startswith("R"):foriinrange(x+1,x+amt+1):path.append((i,y))coord['x']+=amtelifdir.startswith("L"):foriinrange(x-1,x-amt-1,-1):path.append((i,y))coord['x']-=amtelifdir.startswith("U"):foriinrange(y+1,y+amt+1):path.append((x,i))coord['y']+=amtelifdir.startswith("D"):foriinrange(y-1,y-amt-1,-1):path.append((x,i))coord['y']-=amtelse:# Fortunately we don't have any of these
print("Error! Wasn't expecting that!")returnpathdeffindDistancesToCommonPoints(path,intersections):positions={}previousCoord=(0,0)cumulativeDistance=0foriinrange(0,len(path)):cumulativeDistance+=calculateDistance(previousCoord,path[i])ifpath[i]inintersectionsandpath[i]notinpositions:positions[path[i]]=cumulativeDistance# make the current coord the previous one for the next iteration
previousCoord=path[i]returnpositionsdefcalculateDistance(point1,point2):returnabs(point1[0]-point2[0])+abs(point1[1]-point2[1])# Initialize: open file, put both wire directions into lists
withopen('day03.txt')asf:wireOneDirections=f.readline().split(",")wireTwoDirections=f.readline().split(",")# Setup: get the paths that the wires take, put the coordinate points into a trackable list
wireCoord={'x':0,'y':0}firstpath=[]foriinrange(0,len(wireOneDirections)):move(wireOneDirections[i],wireCoord,firstpath)wireCoord={'x':0,'y':0}secondpath=[]foriinrange(0,len(wireTwoDirections)):move(wireTwoDirections[i],wireCoord,secondpath)# Part One: find the intersection point closest to the central port (which is at (0, 0))
intersections=set(firstpath).intersection(set(secondpath))# (Initialize with large number...one that will be larger than any of the numbers we might encounter)
least=1000000fortupinintersections:sum=abs(tup[0])+abs(tup[1])ifsum<least:least=sumprint("Part One: %i"%least)# Part Two: find the fewest combined steps the wires must take to reach an intersection
distancesOne=findDistancesToCommonPoints(firstpath,intersections)distancesTwo=findDistancesToCommonPoints(secondpath,intersections)combinedDistances=[]forkeyindistancesOne:combinedDistances.append(distancesOne[key]+distancesTwo[key])print("Part Two: %i"%min(combinedDistances))
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'm using AoC this year to learn Python. Last year I used it to learn nodejs.
Day 3 was a doozy for me, partly because I started it at 11pm (I'm on American Central Time), but partly because I overthought the problem and thought I would map the whole thing as a 2D array. After screwing my head on a little bit more, I went with simply tracking the path of the wire with x/y tuples, then finding the intersections between the two wires. Manhattan distance was baked into each tuple (just add the absolute values of x and y together and voila).
For my first crack at Part One, I originally used sets. When I discovered Part Two, I just had to change the sets to lists and then I got path distance for free.