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
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 10- Show Best Posts
- Show All (oldest first)
- Show All (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