mwilsoncoding
Advent of Code 2024 - Day 3
Here are the solutions I came up with for today:
Part 1:
defmodule Day3.Part1 do
def solve() do
File.stream!("03/input.txt")
|> Enum.reduce(
0,
fn line, acc ->
acc +
(~r/mul\((\d{1,3}),(\d{1,3})\)/
|> Regex.scan(line |> String.trim(), capture: :all_but_first)
|> Enum.reduce(
0,
fn [a, b], acc -> acc + String.to_integer(a) * String.to_integer(b) end
))
end
)
|> IO.puts()
end
end
Day3.Part1.solve()
Part 2:
defmodule Day3.Part2 do
defp sum_part(part) do
~r/mul\((\d{1,3}),(\d{1,3})\)/
|> Regex.scan(part, capture: :all_but_first)
|> Enum.reduce(
0,
fn [a, b], acc -> acc + String.to_integer(a) * String.to_integer(b) end
)
end
def solve() do
File.stream!("03/input.txt")
|> Enum.reduce(
{0, true},
fn line, {acc, start_enabled?} ->
{acc +
(line
|> String.trim()
|> String.split("do")
|> (fn parts ->
if start_enabled? do
parts
else
parts
|> Enum.drop(1)
end
end).()
|> Enum.filter(&(not String.starts_with?(&1, "n't()")))
|> Enum.map(&sum_part/1)
|> List.to_tuple()
|> Tuple.sum()),
~r/do\(\)/
|> Regex.scan(line, capture: :all, return: :index)
|> List.last()
|> List.last()
|> elem(0) >
~r/don't\(\)/
|> Regex.scan(line, capture: :all, return: :index)
|> List.last()
|> List.last()
|> elem(0)}
end
)
|> elem(0)
|> IO.puts()
end
end
Day3.Part2.solve()
Trending in Challenges
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
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
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Marked As Solved
ruslandoga
I think there is a topic for day 3 already: Advent of Code 2024 - Day 3
Also Liked
ruslandoga
Ah, right! Sorry, I forgot about your comment: Advent of Code 2024 - Day 3 - #3 by mwilsoncoding
Seems like Elixir forum experienced a race condition