mwilsoncoding
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ruslandoga
I think there is a topic for day 3 already: Advent of Code 2024 - Day 3
mwilsoncoding
Indeed. Not sure how to delete this topic. I was up last night and posted as soon as I finished. Unfortunately, so did a lot of other people and the other thread popped up while mine was still under admin review.
If an admin sees this, please remove this topic.
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