bjorng
Erlang Core Team
defmodule Day04 do
def part1(input) do
grid = parse(input)
Enum.count(removable(grid))
end
def part2(input) do
grid = parse(input)
remove(grid, 0)
end
defp remove(grid, num_removed) do
case removable(grid) do
[] ->
num_removed
[_|_]=ps ->
grid = Enum.reduce(ps, grid, &(Map.delete(&2, &1)))
remove(grid, num_removed + length(ps))
end
end
defp removable(grid) do
grid
|> Enum.filter(fn {_position, what} -> what === ?@ end)
|> Enum.filter(fn {position, _} ->
adjacent_squares(position)
|> Enum.count(fn position->
get_grid(grid, position) === ?@
end)
|> then(&(&1 < 4))
end)
|> Enum.map(&elem(&1, 0))
end
defp get_grid(grid, position) do
Map.get(grid, position, ?.)
end
defp adjacent_squares({row, col}) do
[{row - 1, col - 1}, {row - 1, col}, {row - 1, col + 1},
{row, col - 1}, {row, col + 1},
{row + 1, col - 1}, {row + 1, col}, {row + 1, col + 1}]
end
defp parse(input) do
parse_grid(input)
end
defp parse_grid(grid) do
grid
|> Enum.with_index
|> Enum.flat_map(fn {line, row} ->
String.to_charlist(line)
|> Enum.with_index
|> Enum.flat_map(fn {char, col} ->
position = {row, col}
[{position, char}]
end)
end)
|> Map.new
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)
lud
I love immutability because you can just compare a term against its previous version without managing the copy yourself:
In context:
DavidB
Utils function ( always have grid for AOC so I’m re-using those )
vkryukov
I already had a module for working with 2D arrays defined for prior AOC runs, so today’s was straightforward:
mudasobwa
I did my best to never construct a matrix, binaries rock. Also,
Stream.iterate(&calc/1)is a lovely approach to code reuse.Aetherus
Omitting input parsing code because I think it’s trivial.
coordsin the following code is aMapSet.t({i, j})where{i, j}'s are the coordinates of paper scrolls (@) only.Part 1
Part 2
The idea is to prioritize the removal of the scroll with fewest neighbors.
Appendix: MinHeap impl
sevenseacat
This was pretty easy - I also have a
Gridmodule in my app that converts 2D grids into a map of%{{row, col} => val}.https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2025/day04.ex
My initial implementation for part 2 was super naive, and checked all rolls present in the grid on every pass. But you only need to check rolls that are adjacent to rolls that were removed on the last pass, which cut my run time from 230ms to 30ms.
rvnash
Pretty easy. I’ve got to look at all these grid modules people are using. For me I just stored all the indices in a Map and used their presence in the map to indicate where the rolls of paper are.
ken-kost
Finally an easier one.
rvnash
Nice. Question: What Array module are you using here? Home grown? Last year I was using
:arraysfrom hex packages, but it doesn’t compile anymore w/ Elixir 1.19.hauleth
Parse
Implementation
Part 1
Part 2
At first I was removing rolls one by one in P2, but that was super slow. After changing it to do everything at once, then it became much faster (90s+ vs 0.2s).