KeithFrost

KeithFrost

Advent of Code 2025 - Day 3

2025 Dec 03

Lobby

defmodule Joltage do
  def parse_line(s) do
    String.trim(s)
      |> to_charlist()
      |> Enum.map(fn ch ->
        if ch <= ?9 and ch >= ?0 do
          ch - ?0
        end
      end)
  end

  def parse(lines) do
    Enum.map(lines, &parse_line/1)
  end

  def max_prefix(bank) do
    [_last | rbank] = Enum.reverse(bank)
    Enum.max(rbank)
  end

  def max_joltage(bank) do
    max1 = max_prefix(bank)
    [_max1 | tail] = Enum.drop_while(bank, fn j -> j < max1 end)
    max1 * 10 + Enum.max(tail)
  end

  def sum_max_joltages(banks) do
    Enum.reduce(banks, 0, fn bank, sum ->
      sum + max_joltage(bank)
    end)
  end
end
test_banks = """
987654321111111
811111111111119
234234234234278
818181911112111
""" |> String.split("\n", trim: true)
  |> Joltage.parse()
  |> IO.inspect()
Enum.map(test_banks, &Joltage.max_joltage/1)
  |> IO.inspect(charlists: :as_lists)
Joltage.sum_max_joltages(test_banks)
input_banks = File.stream!(__DIR__ <> "/dec-03-input.txt")
  |> Joltage.parse()
Joltage.sum_max_joltages(input_banks)

Part Two

defmodule Joltage2 do
  def max_prefix(bank, n) do
    Enum.reverse(bank)
      |> Enum.drop(n - 1)
      |> Enum.max()
  end
  
  @batteries 12

  def max_joltage(bank, n \\ @batteries, acc \\ 0) do
    if n < 1 do
      acc
    else
      max1 = max_prefix(bank, n)
      [_max1 | tail] = Enum.drop_while(bank, fn j -> j < max1 end)
      max_joltage(tail, n - 1, acc * 10 + max1)
    end
  end

  def sum_max_joltages(banks) do
    Enum.reduce(banks, 0, fn bank, sum ->
      sum + max_joltage(bank)
    end)
  end
end
Enum.map(test_banks, &Joltage2.max_joltage/1)
  |> IO.inspect(charlists: :as_lists)
Joltage2.sum_max_joltages(test_banks)
Joltage2.sum_max_joltages(input_banks)

Most Liked

hauleth

hauleth

Parse

batteries =
  puzzle_input
  |> String.split("\n", trim: true)
  |> Enum.map(fn row ->
    row
    |> String.to_charlist()
    |> Enum.map(& &1 - ?0)
  end)

Implementation

defmodule Joltage do
  def make_largest(list, n) do
    to_remove = length(list) - n
    Enum.reduce(1..to_remove, list, fn _, acc -> make_larger(acc) end)
  end
  
  def make_larger([_]), do: []
  def make_larger([a, b | rest]) when a < b, do: [b | rest]
  def make_larger([b | rest]), do: [b | make_larger(rest)]
end

Part 1

Enum.sum_by(batteries, &Integer.undigits(Joltage.make_largest(&1, 2)))

Part 2

Enum.sum_by(batteries, &Integer.undigits(Joltage.make_largest(&1, 12)))

All thanks to simple observation that to make number larger you need to drop first digit that is smaller than the next one or last digit. Now apply it N times to be left with just required amount of digits and you are good to go.

10
Post #7
vkryukov

vkryukov

The algorithm is pretty straightforward: to take k largest batteries from a list of n, take the earliest largest digit among the first (n - k + 1), then iterate for all the digits after that one.

defmodule Y2025.Day03 do
  def digits(s) do
    s
    |> String.graphemes()
    |> Enum.map(&String.to_integer(&1))
  end

  def first_digit(digits, k) do
    n = length(digits)

    digits
    |> Enum.take(n - k + 1)
    |> Enum.with_index()
    |> Enum.max_by(fn {a, i} -> {a, -i} end)
  end

  def max_digits(digits, k) do
    k..1//-1
    |> Enum.reduce({digits, 0}, fn k, {digits, acc} ->
      {d, pos} = first_digit(digits, k)
      {digits |> Enum.drop(pos + 1), acc * 10 + d}
    end)
    |> elem(1)
  end

  def max_joltage(s, k \\ 2) do
    digits(s)
    |> max_digits(k)
  end

  def part1(s, k \\ 2) do
    s
    |> String.split("\n")
    |> Enum.map(&max_joltage(&1, k))
    |> Enum.sum()
  end

  def part2(s) do
    part1(s, 12)
  end
end
BartOtten

BartOtten

Here we go.

Who knows?
How to do a ‘selective capture’?

The reducer passes 2 arguments to the anonymous function. Thought I could simply do (&2) but that is not allowed. &1 has to be used. The dirty trick is to (&2 || &1) when you are certain &2 will never be falsy but it is stretching the limits.

Could have gone with a simple fn but am wondering if someone knows a nice solid trick.

defmodule Aoc2025.Solutions.Y25.Day03 do
  alias AoC.Input

  def parse(input, _part) do
    Input.read!(input)
    |> String.trim()
    |> String.split("\n")
    |> Enum.map(&String.to_charlist/1)
  end

  def part_one(problem) do
    solve(problem, 2)
  end

  def part_two(problem) do
    solve(problem, 12)
  end

  def solve(problem, limit) do
    problem
    |> Stream.map(&keep_highest(&1, limit))
    |> Stream.map(&to_string/1)
    |> Stream.map(&String.to_integer/1)
    |> Enum.sum()
  end

  def keep_highest(bank, limit) do
      discard = length(bank) - limit
      Enum.reduce(1..discard, bank, &maximize/2)
  end

  def maximize(_reduction, bank), do: maximize(bank)
  def maximize([x, s]), do: [max(x, s)]
  def maximize([l, r | rest]) when l < r, do: [r | rest]
  def maximize([l, r | rest]), do: [l | maximize([r | rest])]
end

Edit 1: You can play “spot the differences” with @hauleth solution :slight_smile:

Where Next?

Popular in Challenges Top

adamu
I said I was on a break, but I took a sneak peak and it looked fun so… Part 1 completes in half a millisecond with a single pass of the ...
New
cblavier
Hey there :wave: No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
sasajuric
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
igorb
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
bjorng
This topic is about Day 5 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
rugyoga
part 1: https://github.com/rugyoga/aoc2021/blob/main/day8.exs part 2: https://github.com/rugyoga/aoc2021/blob/main/day8b.exs
New
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
mattbaker
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
connorlay
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018. To prevent people from being spoiled about s...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New

Other popular topics Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement