bjorng
Erlang Core Team
Advent of Code 2025 - Day 5
Easy puzzles two days in a row. I suspect tomorrow’s puzzle will not be that easy…
defmodule Day05 do
def part1(input) do
{ranges, ingredients} = parse(input)
Enum.count(ingredients, &in_any_range?(&1, ranges))
end
defp in_any_range?(ingredient, ranges) do
Enum.any?(ranges, &(ingredient in &1))
end
def part2(input) do
{ranges, _ingredients} = parse(input)
ranges = ranges
|> Enum.sort
|> combine_ranges
Enum.reduce(ranges, 0, &(Range.size(&1) + &2))
end
defp combine_ranges([r]), do: [r]
defp combine_ranges([r1, r2 | rest]) do
case Range.disjoint?(r1, r2) do
true ->
[r1 | combine_ranges([r2 | rest])]
false ->
r = min(r1.first, r2.first) .. max(r1.last, r2.last)
combine_ranges([r | rest])
end
end
defp parse([ranges, ingredients]) do
ranges = Enum.map(ranges, fn range ->
[first, last] = String.split(range, "-")
String.to_integer(first) .. String.to_integer(last)
end)
ingredients = Enum.map(ingredients, &String.to_integer(&1))
{ranges, ingredients}
end
end
Trending in Challenges
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 10 of 18 Posts
mudasobwa
For the first time I needed a helper private function with multiple heads to handle a recursion properly.
vkryukov
Yep, today was an easy one; parsing takes almost more lines than the rest of the code :).
vkryukov
Nice - TIL that Range module exists :). cc: @bjorng
sevenseacat
https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2025/day05.ex
I don’t want to say I’m disappointed by the puzzles this year, but… so far I kind of am
I expected at least one curly one so far… maybe tomorrow!
hauleth
That is why I created
RangeSet:Parse
Part 1
Part 2
lud
Same solution as everyone I guess
Except @hauleth 's , that RangeSet could come in handy for some other puzzles as well
I have the same thing but for rectangles, there is always that puzzle each year 
BartOtten
No Range was harmed during development
rvnash
It was nice that I got to use Range, and pattern matching on Range’s. I’ve never done that before. I didn’t try it, but I suspect that if tried to do something like accumulate all the values into a MapSet or something, it would take forever and use up all your memory.
Edit: Duh, of course it would with over 300 trillion values.
rvnash
RangeSet is awesome!
dompdv
The key is to sort the ranges. Too bad I’ve not thought about doing so
. The result is, of course, far too complex.