202412080000 Advent of Code 2024 Day 08
Resonant Collinearity
Today we're visiting the roof of a top-secret Easter Bunny installation and need to put a stop to their plot to get people to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift.
#blog #projectThe Puzzle
Today we're visiting the roof of a top-secret Easter Bunny installation and need to put a stop to their plot to get people to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift.
Our puzzle gives us a grid with lettered nodes. We're asked to extrapolate the line that similarly named nodes make. In the first part, we're asked only for the first extrapolation point. For example,
..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
For part 2, we're asked to continue the extrapolation and find all the nodes that fall on the grid. One thing to note is that all nodes of the same letter create antinodes with all other nodes of that letter. The above example shows a simple case. The following is a case with three similarly named nodes.
T....#....
...T......
.T....#...
.........#
..#.......
..........
...#......
..........
....#.....
..........
Solution
I started today by calculating the equation of the line and everything, but luckily realized I was overthinking things. Today's really just boiled down to getting the rise and run of the two points on the line and iterating it out over the grid.
const ;
In the second part, the nodes themselves are considered valid positions of antinodes (which makes sense because they satisfy the equation as well). So we need to add them to the starting set. Also, notice the convenient fact that if we reverse the points to the extrapolate function, we extrapolate out in the other direction.
const ;
;
;
I chose to close over the height
, width
, and iteration limit
for bounds checking in the extrapolate function instead of passing the entire input every time. My late night addled brain just auto-piloted it so that JS wouldn't do that big allocation every time, but it wouldn't actually do that anyway. Functions returning functions can be a little hard to deal with in general, but I think it makes the main loop a little less cluttered. So probably a wash on which is "better".
I took my time with today's and was happy that I did. It was a fun one and not that hard, so just going through it, enjoying it, and getting the right answer was refreshing. Looking forward to tomorrow.
You can find the full solutions to today's AoC puzzles in my AoC GitHub repo.