Advent of Code 2024 - Day 8
Mix.install([
{:kino_aoc, "~> 0.1"}
])
Introduction
— Day 8: Resonant Collinearity —
You find yourselves on the roof of a top-secret Easter Bunny installation.
While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!
Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
So, for these two antennas with frequency a
, they create the two antinodes marked with #
:
..........
...#......
..........
....a.....
..........
.....a....
..........
......#...
..........
..........
Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......#...
..........
..........
Antennas with different frequencies don’t create antinodes; A
and a
count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a
-frequency antinode at its location:
..........
...#......
#.........
....a.....
........a.
.....a....
..#.......
......A...
..........
..........
The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A
-frequency antenna:
......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
Because the topmost A
-frequency antenna overlaps with a 0
-frequency antinode, there are 14
total unique locations that contain an antinode within the bounds of the map.
Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
— Part Two —
Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations.
Whoops!
After updating your model, it turns out that an antinode occurs at any grid position exactly in line with at least two antennas of the same frequency, regardless of distance. This means that some of the new antinodes will occur at the position of each antenna (unless that antenna is the only one of its frequency).
So, these three T
-frequency antennas now create many antinodes:
T....#....
...T......
.T....#...
.........#
..#.......
..........
...#......
..........
....#.....
..........
In fact, the three T
-frequency antennas are all exactly in line with two antennas, so they are all also antinodes! This brings the total number of antinodes in the above example to 9
.
The original example now has 34
antinodes, including the antinodes that appear on every antenna:
##....#....#
.#.#....0...
..#.#0....#.
..##...0....
....0....#..
.#...#A....#
...#..#.....
#....#.#....
..#.....A...
....#....A..
.#........#.
...#......##
Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode?
Puzzle
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "8", System.fetch_env!("LB_AOC_SESSION"))
IO.puts(puzzle_input)
Tools
Tools - Code
defmodule Matrix do
def size(matrix) do
rows = matrix |> length()
cols = matrix |> hd() |> String.length()
{rows, cols}
end
def value(matrix, {x, y}) do
{rows, cols} = size(matrix)
if x >= 0 and x < rows and y >= 0 and y < cols do
matrix
|> Enum.at(y, "")
|> String.at(x)
else
nil
end
end
def distance({x1, y1}, {x2, y2}) do
{x2 - x1, y2 - y1}
end
def add({x1, y1}, {x2, y2}) do
{x1 + x2, y1 + y2}
end
def sub({x1, y1}, {x2, y2}) do
{x1 - x2, y1 - y2}
end
def belongs?(matrix, {x, y}) do
{rows, cols} = size(matrix)
cond do
x < 0 or x >= cols -> false
y < 0 or y >= rows -> false
true -> true
end
end
end
Part One
Code - Part 1
defmodule PartOne do
def solve(input) do
IO.puts("--- Part One ---")
IO.puts("Result: #{run(input)}")
end
def run(input) do
matrix =
input
|> String.split("\n", trim: true)
matrix
|> compute_antenna_points()
|> compute_antinodes(matrix)
|> MapSet.size()
end
def compute_antenna_points(matrix) do
{rows, cols} = Matrix.size(matrix)
for x <- 0..(cols - 1), y <- 0..(rows - 1), reduce: %{} do
acc ->
case Matrix.value(matrix, {x, y}) do
"." -> acc
antenna -> Map.update(acc, antenna, [{x, y}], fn points -> [{x, y} | points] end)
end
end
end
def compute_antinodes(antenna_points, matrix) do
antenna_points
|> Enum.map(fn {_, points} ->
for p1 <- points, p2 <- points, p1 != p2, reduce: [] do
antinodes ->
[compute_antinode(matrix, p1, p2) | antinodes]
end
end)
|> List.flatten()
|> MapSet.new()
end
def compute_antinode(matrix, p1, p2) do
distance = Matrix.distance(p1, p2)
antinode1 = Matrix.sub(p1, distance)
antinode2 = Matrix.add(p2, distance)
[antinode1, antinode2]
|> Enum.filter(fn point -> Matrix.belongs?(matrix, point) end)
end
end
Tests - Part 1
ExUnit.start(autorun: false)
defmodule PartOneTest do
use ExUnit.Case, async: true
import PartOne
@input """
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
@expected 14
test "part one" do
assert run(@input) == @expected
end
end
ExUnit.run()
Solution - Part 1
PartOne.solve(puzzle_input)
Part Two
Code - Part 2
defmodule PartTwo do
def solve(input) do
IO.puts("--- Part Two ---")
IO.puts("Result: #{run(input)}")
end
def run(input) do
matrix =
input
|> String.split("\n", trim: true)
matrix
|> compute_antenna_points()
|> compute_antinodes(matrix)
|> MapSet.size()
end
def compute_antenna_points(matrix) do
{rows, cols} = Matrix.size(matrix)
for x <- 0..(cols - 1), y <- 0..(rows - 1), reduce: %{} do
acc ->
case Matrix.value(matrix, {x, y}) do
"." -> acc
antenna -> Map.update(acc, antenna, [{x, y}], fn points -> [{x, y} | points] end)
end
end
end
def compute_antinodes(antenna_points, matrix) do
antenna_points
|> Enum.map(fn {_, points} ->
for p1 <- points, p2 <- points, p1 != p2, reduce: [] do
antinodes ->
[compute_antinode(matrix, p1, p2) | antinodes]
end
end)
|> List.flatten()
|> MapSet.new()
end
def compute_antinode(matrix, p1, p2) do
{dx, dy} = Matrix.distance(p1, p2)
{rows, cols} = Matrix.size(matrix)
harmonics1 =
for n <- 1..max(rows, cols),
antinode = Matrix.sub(p1, {dx * n, dy * n}),
Matrix.belongs?(matrix, antinode) do
antinode
end
harmonics2 =
for n <- 1..max(rows, cols),
antinode = Matrix.add(p1, {dx * n, dy * n}),
Matrix.belongs?(matrix, antinode) do
antinode
end
[harmonics1, harmonics2]
|> List.flatten()
end
def inf(start) do
Stream.unfold(start, fn
i -> {i, i + 1}
end)
end
end
Tests - Part 2
ExUnit.start(autorun: false)
defmodule PartTwoTest do
use ExUnit.Case, async: true
import PartTwo
@input """
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
@expected 34
test "part two" do
assert run(@input) == @expected
end
end
ExUnit.run()
Solution - Part 2
PartTwo.solve(puzzle_input)