bjorng
Erlang Core Team
Advent of Code 2024 - Day 4
Here is my solution for day 4:
https://github.com/bjorng/advent-of-code/blob/main/2024/day04/lib/day04.ex
Most Liked
BartOtten
Looking at your code I see which rabbit hole you experienced. I experienced the same and just dropped all code and started from scratch.
You made it, which is the most important ![]()
3
woojiahao
Today looked more intimidating than it actually was. General strategy is to bruteforce all directions. We can minimize bruteforce area by only searching when “X” (for part 1) or “A” (for part 2):
defmodule AOC.Y2024.Day4 do
@moduledoc false
use AOC.Solution
@dirs [
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
{-1, -1},
{-1, 1},
{1, -1},
{1, 1}
]
@impl true
def load_data() do
Data.load_day_as_grid(2024, 4)
end
@impl true
def part_one({grid, _, _}) do
grid
|> Enum.filter(fn {_, v} -> v == "X" end)
|> General.map_sum(fn {coord, _} -> count_xmas(grid, coord) end)
end
@impl true
def part_two({grid, _, _}) do
grid
|> Enum.filter(fn {_, v} -> v == "A" end)
|> Enum.count(fn {coord, _} -> has_x_mas(grid, coord) end)
end
defp has_x_mas(grid, {r, c}) do
[tl, tr, bl, br] =
@dirs
|> Enum.slice(4..-1//1)
|> Enum.map(fn {dr, dc} -> {r + dr, c + dc} end)
|> Enum.map(fn coord -> Map.get(grid, coord, ".") end)
([tl, br] == ["M", "S"] or [tl, br] == ["S", "M"]) and
([tr, bl] == ["M", "S"] or [tr, bl] == ["S", "M"])
end
defp count_xmas(grid, {r, c}) do
for {dr, dc} <- @dirs do
0..3
|> Enum.map(fn j -> {r + dr * j, c + dc * j} end)
|> Enum.map_join(fn coord -> Map.get(grid, coord, ".") end)
end
|> Enum.count(fn v -> v == "XMAS" end)
end
end
2
Aetherus
I feel part 2 is actually easier.
defmodule AoC2024.Day04 do
def part_1(grid) do
directions =
for di <- -1..1,
dj <- -1..1,
di != 0 or dj != 0,
do: {di, dj}
directions
|> Enum.map(fn {di, dj} ->
Enum.count(Map.keys(grid), fn coord ->
coord
|> Stream.iterate(fn {i, j} -> {i + di, j + dj} end)
|> Stream.take(4)
|> Enum.map(&grid[&1])
|> Kernel.==(~c"XMAS")
end)
end)
|> Enum.sum()
end
def part_2(grid) do
Enum.count(grid, fn
{{i, j}, ?A} ->
[grid[{i - 1, j - 1}], grid[{i + 1, j + 1}]] in [~c"MS", ~c"SM"] and
[grid[{i - 1, j + 1}], grid[{i + 1, j - 1}]] in [~c"MS", ~c"SM"]
_ ->
false
end)
end
end
where grid is built like this:
charlists = puzzle_input |> String.split() |> Enum.map(&String.to_charlist/1)
grid =
for {row, i} <- Enum.with_index(charlists),
{char, j} <- Enum.with_index(row),
into: %{},
do: {{i, j}, char}
2
Popular in Challenges
Hello, guys. I’m back again, but only for the weekends, maybe.
This topic is about Day 13 of the Advent of Code 2020 .
Thanks to @egze,...
New
This topic is about Day 10 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adv...
New
I have 2 arrays: a1 can be any combination of value or nil like that
a1 = [1,nil,3]
and array 2 the same
a2 = [4,2, nil]
How do I com...
New
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
This topic is about Day 1 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adven...
New
part 1:
https://github.com/rugyoga/aoc2021/blob/main/day8.exs
part 2:
https://github.com/rugyoga/aoc2021/blob/main/day8b.exs
New
Note: This topic is to talk about Day 5 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Today is a brute-force day: advent-of-code-2024/lib/advent_of_code2024/day6.ex at main · ibarakaiev/advent-of-code-2024 · GitHub
Takes a...
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
Other popular topics
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









