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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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.