Aetherus
Advent of Code 2023 - Day 3
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution:
https://github.com/Aetherus/advent-of-code/blob/master/2023/day-03.livemd
Most Liked
seeplusplus
This one took me two hours continuously to get working.
contains_symbol = fn s ->
match = Regex.match?(~r/[^\d.]/, s)
match
end
grab_numbers = fn line ->
Regex.scan(~r/\d+/, line, return: :index)
|> Enum.map(fn [{index, len}] ->
{index, len, line |> String.slice(index, len) |> Integer.parse() |> elem(0)}
end)
end
part1 = fn s ->
for [pre, curr, post] <-
s
|> Enum.reject(&(String.length(&1) == 0))
|> then(fn x -> Enum.concat([""], x) end)
|> Enum.chunk(3, 1)
|> Enum.map(fn u -> Enum.map(u, &String.trim/1) end),
[{index, length}] <- Regex.scan(~r/\d+/, curr, return: :index),
contains_symbol.(String.slice(curr, max(index - 1, 0), length + 2)) ||
contains_symbol.(String.slice(pre, max(index - 1, 0), length + 2)) ||
contains_symbol.(String.slice(post, max(index - 1, 0), length + 2)),
{number, _} = String.slice(curr, index, length) |> Integer.parse() do
number
end
|> Enum.sum()
end
part2 = fn s ->
for [pre, curr, post] <-
s
|> Enum.reject(&(String.length(&1) == 0))
|> Enum.chunk(3, 1)
|> Enum.map(fn u -> Enum.map(u, &String.trim/1) end),
curr |> String.contains?("*"),
[{gear_idx, _}] <- Regex.scan(~r/\*/, curr, return: :index),
matches =
grab_numbers.(pre) |> Enum.concat(grab_numbers.(post)) |> Enum.concat(grab_numbers.(curr)),
valid_matches =
matches
|> Enum.filter(fn {m_idx, m_len, _} ->
gear_idx in max(0, m_idx - 1)..(m_idx + m_len)
end),
valid_matches |> Enum.count() == 2,
[{_, _, a}, {_, _, b}] = valid_matches do
a * b
end
|> Enum.sum()
end
part1.(IO.stream()) |> IO.puts()
4
stevensonmt
I have not yet looked at the solutions in this thread but I just wanted to state that for day 3 this was a very difficult challenge relative to previous years. I think there may have been an effort to handicap the AI coders with the difficult input parsing this year.
4
al2o3cr
+1 for this - it’s my favorite part of AoC.
With “real-world” problems, it usually takes months or years to fully regret an architectural decision - with an AoC problem, all it takes is part 2 ![]()
4
Popular in Challenges
This topic is about Day 5 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]...
New
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
Note: This topic is to talk about Day 3 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can jo...
New
Note: This topic is to talk about Day 16 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
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
Gosh this one took me sooo much time.
At first I was trying to iterate each digit independently on the input A number to make digits cha...
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution: ...
New
This topic is about Day 14 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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









