AOC18: The Stars Align
Advent of Code 2018 Day 10
It’s that time of year again, Christmas is around the corner and Eric Wastl has gifted us with another set of Advent of Code puzzles.
Welcome to Advent Of Code 2018. You can read Part 1 of this series Here or see all the parts of this series Here.
Part 1
Day 10 gives us a list of star positions and velocities, and asks us to track the movements of the stars until they spell out a message. The example message resolves in 3 steps, however we’re told that our actual message will take much longer to appear. We start as usual with reading our input file, splitting it by line, and creating a slice to store our Points in. Our Point type is a struct that contains a point’s x and y position, as well as the x and y velocities. We’ll also setup a regular expression for parsing the data for each point.
|
|
Next we’ll iterate over the lines, and find our regexp matches for each line. If we find a match we’ll convert each element to an integer and append our new point to our slice.
|
|
In order to determine when a message is fully formed, we need to determine what the size of the current message is. As the message is forming, the overall area decreases, since the stars are moving closer together. We should be able to tell if the message is complete, if the stars start diverging, and the area increases. So, we’ll write an absolute value function, which returns the positive of a number it takes in, and a function that will calculate the total area for a message, by finding the minimum and maximum x and y values, and adding them together to get a x and y range.
|
|
Next we’ll need a function to progress our stars. We’ll iterate over the slice and add the x velocity to the x position, and y velocity to the y position.
|
|
Now that we can calculate the size, and move the points, we can setup a loop that will calculate the move the points, and calculate the size, while keeping track of the previous positions. Once we reach a point where the stars start diverging, we’ll break the loop, and our answer will be held in the prevS variable. However we’ll want function to render the message in a readable form.
|
|
Our RenderPoints function finds the size of our message, then creates a two-dimensional slice of strings to store the position of the stars. Once the stars have ben plotted, the function iterates through the Grid, and uses Sprintf to append the current position to a string. This will successfully render the message, however at this point our stars have traveled quite a distance so we’ll need to trim some whitespace from the end of the grid, we’ll iterate over the lines of the msg removing any blank lines and trimming trailing spaces from the line.
|
|
Now that we’re able to render the message, we simply output it for the answer to part 1.
|
|
Part 2
Part 2 asks us exactly how long the message would have taken to resolve, had we not sped up the process by precalculating the paths. This is as simple as adding a counter to our main movement loop and displaying it at the end.
|
|
The complete code for this puzzle is available, along with the other days, on my Gitlab repository https://gitlab.com/aarynsmith/adventofcode/blob/master/2018/Day_10/Day10.go
If you have any questions or comments, please feel free comment below.