seeplusplus
This one was much easier for me than yesterday’s. Part 1 runs in 22ms and part 2 runs in ~3s.
defmodule BridgeRepair do
def parse_input(input) do
input
|> String.trim()
|> String.split("\n")
|> Stream.map(fn l ->
[acc, nums] = String.split(l, ":")
nums = nums |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
{acc |> String.to_integer(), nums}
end)
end
def combine_ints(acc, [], _), do: acc
def combine_ints(acc, [i | rest], ops) do
ops
|> Enum.flat_map(fn op ->
acc |> Enum.map(fn j ->
case op do
:plus -> j + i
:mul -> j * i
:cat -> "#{j}#{i}" |> String.to_integer()
end
end)
end)
|> combine_ints(rest, ops)
end
def part_one_ops() do
[:plus, :mul]
end
def part_two_ops() do
[:cat | part_one_ops()]
end
def solve(input, part) do
input
|> BridgeRepair.parse_input()
|> Stream.filter(fn {sum, [i | rest]} ->
BridgeRepair.combine_ints(
[i],
rest,
(if part == :part1, do: part_one_ops(), else: part_two_ops())
) |> Enum.any?(&(&1 == sum))
end)
|> Stream.map(fn {i, _} -> i end)
|> Enum.sum
end
end
Edit small optimization on the concat operation:
:cat -> j*10**(i |> Integer.digits() |> Enum.count) + i
reduces part 2 runtime to 800ms.
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
lkuty
I like recursion
Back in 1996-98, I did a lot of Scheme (MIT Scheme) and fell in love with that language, FP in general and tail recursion (and TCO of course). We had a teacher in Belgium who went to the USAs and was exposed to all of it. I had a really good time.
Excluding I/O, when I call C for the
produced?function using a NIF, the code executes ± 3 times faster.bjorng
My original version ran in 0.9 seconds for both parts. Since that was a little bit slow for my taste I optimized the concatenation of integers. My first version did it like so:
I rewrote that to operate directly on the integers. That reduced the time to 0.1 seconds for both parts.
The version shown here includes additional refactoring to share the solutions for parts 1 and 2.
https://github.com/bjorng/advent-of-code/blob/main/2024/day07/lib/day07.ex
Aetherus
My solution is to inverse the calculation from right to left, for example:
I try
I accidentally deleted my code ToT
And for the inverse of concatenation, it’s just
lkuty
When you measure time execution, do you take I/O into consideration or not (time taken to read the file) ? I wanted to avoid it but since I am using
StreamI/O and CPU are interleaved.Aetherus
I rewrote my solution.
sevenseacat
Much easier than yesterday! I still went through a few iterations before landing on this one:
https://github.com/sevenseacat/advent_of_code/blob/main/lib/y2024/day07.ex
This takes about 250ms to run on my machine for part 2.
My aim for this year is to have each solution run in less than a second - we’ll see how far I get lol
bjorng
I use the time reported by
mix test.So that includes all I/O, as well as the time for running the examples.
rugyoga
20 lines
woojiahao
Relatively simple day!
cblavier
This was much easier than yesterday!
And I’m always proud of myself when I manage to write recursive code (mostly because I don’t need recursive code very often in my daily job)
Part1
Part2
It takes about 10ms for part1 and
40ms for part2edit: using
Task.async_streamand part 2 is now running in 20ms