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
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
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 just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #performance










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.