Day 9 - Advent of Code 2025
Mix.install([:kino, :benchee])
Links
Prompt
— Day 9: TBD —
You slide down the firepole in the corner of the playground and land in the North Pole base movie theater!
The movie theater has a big tile floor with an interesting pattern. Elves here are redecorating the theater by switching out some of the square tiles in the big grid they form. Some of the tiles are red; the Elves would like to find the largest rectangle that uses red tiles for two of its opposite corners. They even have a list of where the red tiles are located in the grid (your puzzle input).
For example:
7,1
11,1
11,7
9,7
9,5
2,5
2,3
7,3
Showing red tiles as # and other tiles as ., the above arrangement of red tiles would look like this:
..............
.......#...#..
..............
..#....#......
..............
..#......#....
..............
.........#.#..
..............
You can choose any two red tiles as the opposite corners of your rectangle; your goal is to find the largest rectangle possible.
For example, you could make a rectangle (shown as O) with an area of 24 between 2,5 and 9,7:
..............
.......#...#..
..............
..#....#......
..............
..OOOOOOOO....
..OOOOOOOO....
..OOOOOOOO.#..
..............
Or, you could make a rectangle with area 35 between 7,1 and 11,7:
..............
.......OOOOO..
.......OOOOO..
..#....OOOOO..
.......OOOOO..
..#....OOOOO..
.......OOOOO..
.......OOOOO..
..............
You could even make a thin rectangle with an area of only 6 between 7,3 and 2,3:
..............
.......#...#..
..............
..OOOOOO......
..............
..#......#....
..............
.........#.#..
..............
Ultimately, the largest rectangle you can make in this example has area 50. One way to do this is between 2,5 and 11,1:
..............
..OOOOOOOOOO..
..OOOOOOOOOO..
..OOOOOOOOOO..
..OOOOOOOOOO..
..OOOOOOOOOO..
..............
.........#.#..
..............
Using two red tiles as opposite corners, what is the largest area of any rectangle you can make?
To begin, get your puzzle input.
— Part Two —
Part Two prompt
QUESTION TWO?
Although it hasn’t changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3"
Solution
defmodule Day09 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
input
|> parse()
|> Enum.to_list()
|> all_pairs()
|> Enum.map(&calc_area/1)
|> Enum.max()
end
def part2(input) do
input
# |> parse()
# |> Enum.map(& &1)
end
defp all_pairs([h | [t]]), do: {h, t}
defp all_pairs([h | tail]) do
[
Enum.map(tail, &{h, &1}),
all_pairs(tail)
]
|> List.flatten()
end
defp calc_area({{x1, y1}, {x2, y2}}) do
(abs(x2 - x1) + 1) * (abs(y2 - y1) + 1)
end
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.splitter("\n", trim: true)
|> parse()
end
def parse(input) do
Stream.map(input, &parse_line/1)
end
def parse_line(line) do
line
|> String.split(",")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end
end
end
{:module, Day09, <<70, 79, 82, 49, 0, 0, 12, ...>>, ...}
Using two red tiles as opposite corners, what is the largest area of any rectangle you can make?
Your puzzle answer was 4755064176.
Day09.part1(input)
50
QUESTION TWO?
Your puzzle answer was answer two.
Day09.part2(input)
"7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3"
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
Tests
ExUnit.start(auto_run: false)
defmodule Day09Test do
use ExUnit.Case, async: false
setup_all do
[
input: "7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3"
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day09.part1(input) == 50
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day09.part2(input) == input
end
end
end
ExUnit.run()
Running ExUnit with seed: 900306, max_cases: 16
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures
%{total: 2, failures: 0, excluded: 0, skipped: 0}
Benchmarking
defmodule Benchmarking do
# https://github.com/bencheeorg/benchee
def run(input) do
Benchee.run(
%{
"Part 1" => fn -> Day09.part1(input) end,
"Part 2" => fn -> Day09.part2(input) end
},
memory_time: 2,
reduction_time: 2
)
nil
end
end
nil
Benchmarking.run(input)