Advent of Code: Day 3

Day 3’s problem was an interesting one. We are given two wires and have to find the point where the two wires intersect that’s closest to the origin. As usual, we’ll approach this problem in two steps. First we’ll parse the input. Then’ll we’ll solve the actual problem.

Before we can get to writing our parsing logic, we’ll need to define two types. These are the types we’ll use to represent the parsed input. We’ll have a wire type that represents a full wire. We’ll also have a wireSegment type that represents an individual segment of the wire:

We’ll also define a readInput function that reads the input file and parses the two wires. This is pretty similar to previous readInput functions we’ve written that have parsed a list of numbers. This time, instead of using strconv.Atoi to parse a number, we’ll write our own parseWire function that will take a line and return the wire on it:

The parseWire function is pretty similar. Split the line by comma to get the string representation of each segment. Then for each segment, pull out the direction (the first character) and the length of the segment (everything but the first character):

With our parseInput function complete, we can move onto our main function. Again, our main function will have a similar structure to the other main functions we’ve written so far for the AoC:

Ok. Now that we’ve got our parsing complete, we can start thinking about how to solve the problem. We need to find the intersection point of the two wires that’s closest to the origin. Thinking for a bit, if we had a list of points where the two wires intersected,  we could easily determine which point is closest to the origin. Unfortunately, there doesn’t appear to be an easy way to determine the intersection points with the wires represented as a list of segments.

Although, what we can do is convert the wires into a format that’s better for calculating the intersection. We can convert each wire into the set of points the wire goes through. With the two point sets, we can easily intersect the two sets and find the point closest to the origin. Let’s try to implement this strategy!

The first thing we need to do is write a function that converts a wire to a set. To do so, we can start at the origin and trace the path made by each line segment. For a given line segment, we take segment.length steps in the direction segment.dir. We do this for each segment. For every point we hit along the way, we add it to a set of points. The end result looks something like:

With wireToSet we can now write our solve function. We first need to convert our wires into sets of points. Then we can iterate through one of the sets and check if it’s in the other set to see if there’s an intersection at that point. If there is an intersection, we can check if that intersection point is closer than any intersection point we’ve seen before. That way we can find the closest intersection point! We can implement this strategy with just a few lines of code:

And that’s it for part 1!

For part 2 we have a similar, but slightly different problem. Instead of needing to find the intersection point closest to the origin, we need to find the intersection point that occurs closest to the start of the wires, measured by the sum of the distance to the point from the start of each wire.

The first idea that came to my mind was to iterate through the points in such a way that as soon as we find a point on both wires, we know that that intersection is the intersection closest to the start of the wires. I’m not exactly sure how this idea would work and it doesn’t sound easy to implement so I started to look for other strategies.

It seems kind of necessary to implement some way to iterate through each wire so we can find how far along each wire each point is. Thinking some more, I realized we don’t need to iterate through the wires in any fancy way, the only value we care about is how far each point is along the wire. That gives us an idea of how we can solve this problem.

What we can do is iterate through each wire. This time, instead of constructing a set of points the wire goes through, we construct a map that tells us for each point, how far along the wire that point is. We can construct these maps for each wire, intersect them, and find the point with the lowest sum distance! This requires only a few modifications to our existing code. We can change our wireToSet function to wireToMap. We add a few bits of code to keep track of our current distance along the wire.

Now that we’re tracking the distance from the start of each wire to each point, we can use the same solve function as before, but now we change our distance function to be the sum of the distance to each point:

And that gives us our solution to part 2! I thought this was another great Advent of Code problem. If you want to view the full source code for my solutions, you can find my code on my GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *