Aetherus

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

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()
stevensonmt

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.

al2o3cr

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 :stuck_out_tongue:

Where Next?

Popular in Challenges Top

Aetherus
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
kwando
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
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
liamcmitchell
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
bjorng
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
bjorng
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
connorlay
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
lud
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
Aetherus
Don’t know why the regex ~r/[\W &amp;&amp; [^\.]]/x does not work in Elixir. It works pretty well in Ruby. Anyway, here is my solution: ...
New
bjorng
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 Top

vertexbuffer
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
jerry
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
belgoros
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
joeerl
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
boundedvariable
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
lanycrost
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement