Powered by AppSignal & Oban Pro

Day 2 - Advent of Code 2025

lib/advent_of_code/2025/day-02.livemd

Day 2 - Advent of Code 2025

Mix.install([:kino, :benchee])

Links

Prompt

— Day 2: Gift Shop —

You get inside and take the elevator to its only other stop: the gift shop. “Thank you for visiting the North Pole!” gleefully exclaims a nearby sign. You aren’t sure who is even allowed to visit the North Pole, but you know you can access the lobby through here, and from there you can access the rest of the North Pole base.

As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for your help.

As it turns out, one of the younger Elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database! Surely, it would be no trouble for you to identify the invalid product IDs for them, right?

They’ve even checked most of the product ID ranges already; they only have a few product ID ranges (your puzzle input) that you’ll need to check. For example:

11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
824824821-824824827,2121212118-2121212124

(The ID ranges are wrapped here for legibility; in your input, they appear on a single long line.)

The ranges are separated by commas (,); each range gives its first ID and last ID separated by a dash (-).

Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.

None of the numbers have leading zeroes; 0101 isn’t an ID at all. (101 is a valid ID that you would ignore.)

Your job is to find all of the invalid IDs that appear in the given ranges. In the above example:

  • 11-22 has two invalid IDs, 11 and 22.
  • 95-115 has one invalid ID, 99.
  • 998-1012 has one invalid ID, 1010.
  • 1188511880-1188511890 has one invalid ID, 1188511885.
  • 222220-222224 has one invalid ID, 222222.
  • 1698522-1698528 contains no invalid IDs.
  • 446443-446449 has one invalid ID, 446446.
  • 38593856-38593862 has one invalid ID, 38593859.
  • The rest of the ranges contain no invalid IDs.

Adding up all the invalid IDs in this example produces 1227775554.

What do you get if you add up all of the invalid IDs?

To begin, get your puzzle input.

— Part Two —

The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young Elf was doing other silly patterns as well?

Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs.

From the same example as before:

  • 11-22 still has two invalid IDs, 11 and 22.
  • 95-115 now has two invalid IDs, 99 and 111.
  • 998-1012 now has two invalid IDs, 999 and 1010.
  • 1188511880-1188511890 still has one invalid ID, 1188511885.
  • 222220-222224 still has one invalid ID, 222222.
  • 1698522-1698528 still contains no invalid IDs.
  • 446443-446449 still has one invalid ID, 446446.
  • 38593856-38593862 still has one invalid ID, 38593859.
  • 565653-565659 now has one invalid ID, 565656.
  • 824824821-824824827 now has one invalid ID, 824824824.
  • 2121212118-2121212124 now has one invalid ID, 2121212121.

Adding up all the invalid IDs in this example produces 4174379265.

What do you get if you add up all of the invalid IDs using these new rules?

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()
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"

Solution

defmodule Day02 do
  defdelegate parse(input), to: __MODULE__.Input

  @part1_regex ~r/^([0-9]+)\1$/
  def part1(input) do
    input
    |> parse()
    |> Enum.flat_map(&find_invalid_ids(&1, @part1_regex)) 
    |> Enum.sum()
  end

  @part2_regex ~r/^([0-9]+)\1+$/
  def part2(input) do
    input
    |> parse()
    |> Enum.flat_map(&find_invalid_ids(&1, @part2_regex))
    |> Enum.sum()
  end

  defp find_invalid_ids({low, high}, regex) do
    low..high
    |> Stream.map(&to_string/1)
    |> Stream.filter(&is_invalid?(&1, regex))
    |> Stream.map(&String.to_integer/1)
  end

  defp is_invalid?(input, regex) do
    Regex.match?(regex, input)
  end

  defmodule Input do
    def parse(input) when is_binary(input) do
      input
      |> parse_line()
    end

    def parse_line(line) do
      line
      |> String.split(["-", ","], trim: true)
      |> Enum.map(&String.to_integer/1)
      |> Enum.chunk_every(2)
      |> Enum.map(&List.to_tuple/1)
    end
  end
end
{:module, Day02, <<70, 79, 82, 49, 0, 0, 14, ...>>,
 {:module, Day02.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}

What do you get if you add up all of the invalid IDs?

Your puzzle answer was 19605500130.

Day02.part1(input)
1227775554

What do you get if you add up all of the invalid IDs using these new rules?

Your puzzle answer was 36862281418.

Day02.part2(input)
4174379265

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 Day02Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input: "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input: input} do
      assert Day02.part1(input) == 1227775554
    end
  end

  describe "part2/1" do
    test "returns expected value", %{input: input} do
      assert Day02.part2(input) == 4174379265
    end
  end
end

ExUnit.run()
Running ExUnit with seed: 456037, max_cases: 28

..
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 -> Day02.part1(input) end,
        "Part 2" => fn -> Day02.part2(input) end
      },
      memory_time: 2,
      reduction_time: 2
    )

    nil
  end
end
{:module, Benchmarking, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:run, 1}}
Benchmarking.run(input)
Operating System: macOS
CPU Information: Apple M4 Pro
Number of Available Cores: 14
Available memory: 48 GB
Elixir 1.18.3
Erlang 27.3
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 1          0.90         1.12 s     ±0.41%         1.12 s         1.12 s
Part 2          0.86         1.16 s     ±0.83%         1.16 s         1.17 s

Comparison: 
Part 1          0.90
Part 2          0.86 - 1.04x slower +0.0463 s

Memory usage statistics:

Name           average  deviation         median         99th %
Part 1       251.83 MB     ±0.00%      251.83 MB      251.83 MB
Part 2       251.85 MB     ±0.00%      251.85 MB      251.85 MB

Comparison: 
Part 1       251.83 MB
Part 2       251.85 MB - 1.00x memory usage +0.0217 MB

Reduction count statistics:

Name   Reduction count
Part 1         65.25 M
Part 2         67.30 M - 1.03x reduction count +2.05 M

**All measurements for reduction count were the same**
nil