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











Most Liked- 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:
dompdv
This one was pretty straightforward. Using MapSet (and MapSet operations) and the “for” construct, which is more concise than reduce. No optimization at all
mudasobwa
I did my best to never construct a matrix, binaries rock. Also,
Stream.iterate(&calc/1)is a lovely approach to code reuse.Last Post!
billylanchantin