bjorng
Erlang Core Team
“When in doubt, use brute force.” – Ken Thompson
defmodule Day02 do
def part1(input) do
solve(input, &invalid_part1?/1)
end
defp invalid_part1?(n) when is_integer(n) and n > 0 do
cond do
n in 10..99 ->
div(n, 10) === rem(n, 10)
n in 1000..9999 ->
div(n, 100) === rem(n, 100)
n in 100_000..999_999 ->
div(n, 1000) === rem(n, 1000)
n in 10_000_000..99_999_999 ->
div(n, 10_000) === rem(n, 10_000)
n in 1_000_000_000..9_999_999_999 ->
div(n, 100_000) === rem(n, 100_000)
true -> false
end
end
def part2(input) do
solve(input, &invalid_part2?/1)
end
defp invalid_part2?(n) when is_integer(n) and n > 0 do
powers = [10, 100, 1000, 10000, 100000, 1000000]
Enum.any?(powers, fn power ->
part = rem(n, power)
if part < div(power, 10) do
false
else
case count_parts(div(n, power), power, part, 1) do
nil -> false
num_parts -> num_parts >= 2
end
end
end)
end
defp count_parts(0, _power, _part, num_parts), do: num_parts
defp count_parts(n, power, part, num_parts) do
case rem(n, power) do
^part ->
count_parts(div(n, power), power, part, num_parts + 1)
_ ->
nil
end
end
defp solve(input, invalid) do
parse(input)
|> Enum.flat_map(&expand_range(&1, invalid))
|> Enum.sum
end
defp expand_range(r, invalid) do
Enum.flat_map(r, fn n ->
case invalid.(n) do
true -> [n]
false -> []
end
end)
end
defp parse(input) do
input
|> Enum.flat_map(fn line ->
line
|> String.split(",")
|> Enum.map(fn range ->
range
|> String.split("-")
|> then(fn [first, last] ->
String.to_integer(first) .. String.to_integer(last)
end)
end)
end)
end
end
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)
code-shoily
I just halved the string and checked for both halves being equal for part 1 and good old regex for part 2.
KeithFrost
2025 Dec 02
Gift Shop
Part Two
everte
I’ve also gone for the brute-force option. Not very elegant and slow (3s or so), luckily the input didn’t make it impossible to just brute force it so I didn’t have to think hard about a clever solution (although I am intrigued and hope to see some clever ways here later today!).
lud
Yes brute force is the way
Edit: I realize now that
mirror?is a very incorrect name for that functionhauleth
Today is brute day:
mudasobwa
No regexes, no magic save for metaprogramming.
tnlogy
Nice to see alternative solutions, as a first time use of Tasks I expected my naive version with async_stream to be faster than my slow solution, but it’s even slower. Why is that?
sevenseacat
https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2025/day02.ex
I did this for part 1 too, but converted it to
Integer.digits(num)and then chunking the resulting list into different sizes. Totally brute force but with some async goodies, part 2 runs in 0.4 seconds on my M1.rvnash
I guess I did the brute force way too, as part 2 takes about 3 seconds. Who here has the more efficient solution? I can’t imagine what that might be.
rvnash
Yup, this is way faster than mine!
Edit: This is a great example of how fast and efficient pattern matching must be inside the BEAM.