Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 8
Day 8: Resonant Collinearity
https://adventofcode.com/2024/day/8
On top of the building you are on is a radio antenna, and it is up to no good. There are many more like it across the city.
A pair of antennas broadcasting the same wavelength create two antinodes (spots where the nefarious purposes of the antenna are realised, making people buy easter bunny stuff).
An example input looks like this:
It’s a 2D-grid of the city. Each alphanumeric character is an antenna. Identical characters send out identical wavelengths.
Parsing
I turned the input string into a few datapoints, the bounds of the grid (rows and cols), and a mapping from each type of radiotower to a list of points where a tower of that type exists.
Helper structure
In a previous day, I said I’d start parsing data into neat data structures in order to get confused less often.
I had some trouble keeping track if index 0 was a row index or a column index, and was x
the variable for column again? Oh no, that was y
…
So, Point
!
This is not necessary at all, it probably even hurts performance a tiny bit, but it’s way nicer to show and understand code.
Parsing code
For convenience, I turned all numbers here into numbers that can go negative again, because before typing this post I solved the day, and we get to do some offset calculating again!
Part 1
A pair of the same antenna types create 2 antinodes, one on each side of the antennas. That happens at 2 locations directly in line with the antennas.
For a location to be an antinode, it has to be exactly twice as far away from one antenna as it is from the other.
Visualized, this pair of a
antennas create 2 antinodes marked with #
:
The question asks how many points within the bounds are an antinode.
Helpers
I expanded the Point
structure with some methods.
- To add 2
Point
s together - To calculate the distance between 2
Point
s - To check if a
Point
is within the bounds of the grid
Part 1 code
These helpers make the code for part 1 much simpler to read (as opposed to a bunch of loose numbers) in my opinion.
I iterate over the map of antennas, so each loop has a list of equal antennas.
I then loop over all combinations of 2 Point
s.
The distance between the Point
s is added to the first Point
.
If the resulting Point
is within the bounds of the map, it’s an antinode and is added to a set.
So, per loop, only 1 antinode can be added to the final set, because each pair will appear twice:
- once as (point1, point2)
- once as (point2, point1)
At the end of the loop, the length of that set is the answer to p1!
Part 2
It turns out that an antinode occurs at any grid position exactly in line with at least two antennas of the same frequency.
So there are more than 2 per pair, they keep going until they leave the bounds of the map.
This was a small change!
Adding a while
loop to the part 1 code, adding the distance to a point each time we loop.