KeithFrost
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)
Trending in Challenges
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lud
A bit overengineered but runs under 3ms on my machine so good enough I guess
Aetherus
My top-down dynamic programming solution for both parts (omitting the input parsing part):
Part 1
Part 2
By the way,
inputis likevkryukov
The algorithm is pretty straightforward: to take
klargest batteries from a list ofn, take the earliest largest digit among the first(n - k + 1), then iterate for all the digits after that one.mudasobwa
Brute force, after all.
sevenseacat
This one was a bit of fun!
https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2025/day03.ex
hauleth
Parse
Implementation
Part 1
Part 2
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.
rvnash
Pretty much the same as most others.
Updated to simplify.
everte
Not super happy with my solution today, fairly fiddly with the indexes and off-by-one’s. Will have to see in the topic what solutions work better :).
Enum.sum_by(batteries, &Integer.undigits(Joltage.make_largest(&1, 12)))Oh! I didn’t know about this
Integer.undigits, that helps!DavidB
tnlogy
I learn some more Enum-methods when reading your solutions. This is my 3:rd day using Elixir solution :). Updated using `Integer.undigits` after seeing it here.