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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 21 to 12- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
billylanchantin
Gearhartlove
Thanks for sharing this solution, it helped me out a ton. I love the
Enum.max_by(fn {a, i} -> {a, -i} end)because it’s a great way to select the “left most” maximum. I also didn’t realize that you perform max and min on tuples, great to learn that they compare their values left to right. Thanks again!brownerd
adamu
This is really nice. I also today TIL learned
Integer.undigits/2, although replacing a string round-trip with that didn’t make my solution fasterlkuty
Ooops forgot about
Integer.undigits/2.lkuty
Aetherus
Yes, it is memoization. I don’t know what your definition of tree search is though, cuz there’s no tree in my implementation.
My
memostores the tuple{lights_need_to_turn_on, index}as the keys, andmax_jolt_in_the_subarray_from_index_and_afteras the values.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.&1has 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
fnbut am wondering if someone knows a nice solid trick.Edit 1: You can play “spot the differences” with @hauleth solution
dompdv
My solution, after a first attempt doing a never ending search (I’ve forgotten about memoization: I should memorize memoization more often). I thought my nice optimizations would do it, but finally they didn’t.
I found a more direct way
dompdv
I’d be happy to have a commented code. Your solution looks impressive but I don’t understand at all how it works! Is it a tree search with memoization ?