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()
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 ![]()
1
Popular in Challenges
This topic is about Day 9 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 17 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Took me a minute to remember my binary math :smile: :grimacing:…
import Bitwise
__DIR__
|> Path.join("puzzle.txt")
|> File.stream...
New
Note: This topic is to talk about Day 9 of the Advent of Code.
For general discussion about the Advent of Code 2018 and links to topics ...
New
Note by the Moderators: This topic is to talk about the first day of the Advent of Code.
For general discussion about the Advent of Code...
New
Here is my solution for day 4:
New
This topic is about Day 9 of the Advent of Code 2021 .
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile:
The trick was to first generate a list...
New
Note: This topic is to talk about Day 4 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
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
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
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
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







