Advent of Code 2024, Day 8

A fairly easy day of map parsing, combinatorics, and bounded value generation. A fun one nonetheless!

Old buildings with seemingly makeshift antennas on them.
Photo by Umesh Gopinath on Unsplash

Part of the AoC 2024 series

Again map, parsing, bit of vector stuff and uniqueness in part 1, and a minor modificiation in part 2. Resonance!

adventofcode2024/day08 at main · javorszky/adventofcode2024
Contribute to javorszky/adventofcode2024 development by creating an account on GitHub.

Spoilers

Part 1

Reading the problem, this will be an easy one. Map is a HashMap, parsing antennae into their own HashMap<char, Vec<Coordinate>> is easy enough (list of coordinates where a specific kind of anteanne is).

Finding antinodes is also easy. Get all the nodes per antenna type, generate all the possible pairs in whatever direction, get the difference between them, then add an antinode to either side that’s the same distance away, provided they fit in the map.

Generating the pairs happens by iterating through the same vec, except the second time we start from the next index compared to where we are. In pseudocode it would look like this:

for (i, element) in list {
    for other_element in list[i+1..] {
        (element, other_element); // these are now a pair that we haven't seen before!
    }
}

Small modular testable functions to the rescue! At the end I’m left with a bunch of antinode maps for the different antenna types, so I need to merge them together. That happens by creating a HashSet<Coordinate>, and just blindly inserting all of the antinodes from all of the antinode maps. The HashSet, being a HashSet, will take care of not duplicating data.

Solution is the length of the set. Yay! ⭐️

Part 2

Well this was easy.

All I had to do is modify the function that finds antinodes based on a pair of coordinates.

Previously it only generated the two of them on either side of them, but now it generates all of them within the map. I modified it such that it now takes in the max index of height and width of the map, puts the two coordinates I pass to the function into a vec to start, and then starts generating one way until the next one would go off the map, then the other way until the next one goes off the map.

Everything else in the codebase is exactly the same. Got my ⭐️⭐️ to prove it!

Spoiler photo by Jason Leung on Unsplash