Aetherus
Advent of Code 2024 - Day 5
Today’s problem is really tense. I don’t think I can do it without libgraph.
Most Liked
ruslandoga
Brute Enum.sort_by with String.contains?
def solve(input) do
[rules, pages] = String.split(input, "\n\n")
pages =
pages
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line |> String.split(",") |> Enum.map(&String.to_integer/1)
end)
Enum.reduce(pages, {0, 0}, fn page, {p1, p2} ->
sorted =
Enum.sort_by(page, &Function.identity/1, fn l, r ->
String.contains?(rules, "#{l}|#{r}")
end)
mid = Enum.at(sorted, div(length(sorted), 2))
if page == sorted do
{p1 + mid, p2}
else
{p1, p2 + mid}
end
end)
end
Full: aoc2024/lib/day05.ex at master · ruslandoga/aoc2024 · GitHub
3
igorb
Here’s my approach with a Map: advent-of-code-2024/lib/advent_of_code2024/day5.ex at main · ibarakaiev/advent-of-code-2024 · GitHub
2
cblavier
Easy part 1, I had to think more for part 2. I thought my approach would take an infinite time, but it didn’t (about 40ms)
I implemented a bubble sort like approach:
- traverse the list of numbers
- every time we find an invalid number (compared to all previous ones), swap them
- then recursive call to traverse and fix the new swapped list.
Part 1
defmodule Advent.Y2024.Day05.Part1 do
def run(puzzle) do
{rules, updates} = parse(puzzle)
updates
|> Enum.filter(&valid?(&1, rules))
|> Enum.map(&Enum.at(&1, &1 |> length |> div(2)))
|> Enum.sum()
end
def parse(puzzle) do
[rules, updates] = String.split(puzzle, "\n\n")
rules =
for rule <- String.split(rules, "\n"), reduce: MapSet.new() do
acc ->
[p1, p2] = String.split(rule, "|")
MapSet.put(acc, {String.to_integer(p1), String.to_integer(p2)})
end
updates =
for pages <- String.split(updates, "\n") do
for page <- String.split(pages, ","),
page = String.to_integer(page),
do: page
end
{rules, updates}
end
def valid?(update, rules) do
update
|> Enum.reduce({[], true}, fn
_page, {prev, false} ->
{prev, false}
page, {prev, _valid?} ->
valid? = not Enum.any?(prev, &MapSet.member?(rules, {page, &1}))
{[page | prev], valid?}
end)
|> elem(1)
end
end
Part 2
defmodule Advent.Y2024.Day05.Part2 do
alias Advent.Y2024.Day05.Part1
def run(puzzle) do
{rules, updates} = Part1.parse(puzzle)
updates
|> Enum.reject(&Part1.valid?(&1, rules))
|> Enum.map(&for {p, i} <- Enum.with_index(&1), into: %{}, do: {i, p})
|> Enum.map(&fix_update(&1, rules))
|> Enum.map(&Map.get(&1, &1 |> Enum.count() |> div(2)))
|> Enum.sum()
end
def fix_update(update, rules) do
for i <- 1..(Enum.count(update) - 1), j <- 0..(i - 1), reduce: {nil, true} do
{indexes, false} ->
{indexes, false}
{nil, true} ->
{first, second} = {Map.get(update, j), Map.get(update, i)}
if MapSet.member?(rules, {second, first}) do
{{i, j}, false}
else
{nil, true}
end
end
|> then(fn
{nil, true} -> update
{{i, j}, false} -> update |> swap(i, j) |> fix_update(rules)
end)
end
defp swap(update, i, j) do
update |> Map.put(i, Map.get(update, j)) |> Map.put(j, Map.get(update, i))
end
end
2
Popular in Challenges
I said I was on a break, but I took a sneak peak and it looked fun so…
Part 1 completes in half a millisecond with a single pass of the ...
New
Hey there :wave:
No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
This topic is about Day 5 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adve...
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
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018.
To prevent people from being spoiled about s...
New
Note: This topic is to talk about Day 9 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Other popular topics
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









