Day 5 - Advent of Code 2025
Mix.install([:kino, :benchee])
Links
Prompt
— Day 5: Cafeteria —
As the forklifts break through the wall, the Elves are delighted to discover that there was a cafeteria on the other side after all.
You can hear a commotion coming from the kitchen. “At this rate, we won’t have any time left to put the wreaths up in the dining hall!” Resolute in your quest, you investigate.
“If only we hadn’t switched to the new inventory management system right before Christmas!” another Elf exclaims. You ask what’s going on.
The Elves in the kitchen explain the situation: because of their complicated new inventory management system, they can’t figure out which of their ingredients are fresh and which are spoiled. When you ask how it works, they give you a copy of their database (your puzzle input).
The database operates on ingredient IDs. It consists of a list of fresh ingredient ID ranges, a blank line, and a list of available ingredient IDs. For example:
3-5
10-14
16-20
12-18
1
5
8
11
17
32
The fresh ID ranges are inclusive: the range 3-5 means that ingredient IDs 3, 4, and 5 are all fresh. The ranges can also overlap; an ingredient ID is fresh if it is in any range.
The Elves are trying to determine which of the available ingredient IDs are fresh. In this example, this is done as follows:
-
Ingredient ID
1is spoiled because it does not fall into any range. -
Ingredient ID
5is fresh because it falls into range3-5. -
Ingredient ID
8is spoiled. -
Ingredient ID
11is fresh because it falls into range10-14. -
Ingredient ID
17is fresh because it falls into range16-20as well as range12-18. -
Ingredient ID
32is spoiled.
So, in this example, 3 of the available ingredient IDs are fresh.
Process the database file from the new inventory management system. How many of the available ingredient IDs are fresh?
To begin, get your puzzle input.
— Part Two —
The Elves start bringing their spoiled inventory to the trash chute at the back of the kitchen.
So that they can stop bugging you when they get new inventory, the Elves would like to know all of the IDs that the fresh ingredient ID ranges consider to be fresh. An ingredient ID is still considered fresh if it is in any range.
Now, the second section of the database (the available ingredient IDs) is irrelevant. Here are the fresh ingredient ID ranges from the above example:
3-5
10-14
16-20
12-18
The ingredient IDs that these ranges consider to be fresh are 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. So, in this example, the fresh ingredient ID ranges consider a total of 14 ingredient IDs to be fresh.
Process the database file again. How many ingredient IDs are considered to be fresh according to the fresh ingredient ID ranges?
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()
"3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32"
Solution
defmodule Day05 do
defdelegate parse(input), to: __MODULE__.Input
def part1(input) do
{ranges, ingredients} = parse(input)
Enum.count(ingredients, fn i ->
Enum.any?(ranges, &(i in &1))
end)
end
def part2(input) do
{ranges, _} = parse(input)
Enum.reduce(ranges, [], fn range, acc ->
acc
|> Enum.reduce({range, []}, &combine_ranges/2)
|> then(fn {combined_range, acc} -> [combined_range | acc] end)
end)
|> Enum.sum_by(&Range.size/1)
end
# 3..5, 10..14
# 10..14, 3..5
defp combine_ranges(a..b//1, {c..d//1, acc}) when c > b or a > d,
do: {c..d, [a..b | acc]}
# 12..18, 16..20
# 16..20, 12..18
defp combine_ranges(a..b//1, {c..d//1, acc}) when c <= b and d >= a,
do: {min(a, c)..max(b, d), acc}
defmodule Input do
def parse(input) when is_binary(input) do
[ranges, ingredients] =
input
|> String.split("\n\n", trim: true)
|> Enum.map(&to_lines/1)
{
Enum.map(ranges, &parse_range/1),
Enum.map(ingredients, &String.to_integer/1)
}
end
defp to_lines(input) do
String.split(input, "\n", trim: true)
end
def parse_range(range) do
[a, z] = String.split(range, "-")
String.to_integer(a)..String.to_integer(z)
end
end
end
{:module, Day05, <<70, 79, 82, 49, 0, 0, 14, ...>>, ...}
How many of the available ingredient IDs are fresh?
Your puzzle answer was 874.
Day05.part1(input)
3
Process the database file again. How many ingredient IDs are considered to be fresh according to the fresh ingredient ID ranges?
Your puzzle answer was 348548952146313.
Day05.part2(input)
14
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 Day05Test do
use ExUnit.Case, async: false
setup_all do
[
input: "3-5\n10-14\n16-20\n12-18\n\n1\n5\n8\n11\n17\n32"
]
end
describe "part1/1" do
test "returns expected value", %{input: input} do
assert Day05.part1(input) == 3
end
end
describe "part2/1" do
test "returns expected value", %{input: input} do
assert Day05.part2(input) == 14
end
end
end
ExUnit.run()
Running ExUnit with seed: 145688, 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 -> Day05.part1(input) end,
"Part 2" => fn -> Day05.part2(input) end
},
memory_time: 2,
reduction_time: 2
)
nil
end
end
{:module, Benchmarking, <<70, 79, 82, 49, 0, 0, 8, ...>>, ...}
Benchmarking.run(input)
Operating System: macOS
CPU Information: Apple M1
Number of Available Cores: 8
Available memory: 8 GB
Elixir 1.19.4
Erlang 28.2
JIT enabled: true
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s
Excluding outliers: false
Benchmarking Part 1 ...
Benchmarking Part 2 ...
Calculating statistics...
Formatting results...
Name ips average deviation median 99th %
Part 2 2.84 K 0.35 ms ±16.16% 0.34 ms 0.45 ms
Part 1 0.39 K 2.55 ms ±10.90% 2.54 ms 2.66 ms
Comparison:
Part 2 2.84 K
Part 1 0.39 K - 7.25x slower +2.20 ms
Memory usage statistics:
Name Memory usage
Part 2 1.46 MB
Part 1 0.0862 MB - 0.06x memory usage -1.37627 MB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
Part 2 0.0870 M
Part 1 1.01 M - 11.57x reduction count +0.92 M
**All measurements for reduction count were the same**
nil