seeplusplus
Advent of Code 2024 - Day 7
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.
Most Liked
sevenseacat
Author of Ash Framework
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
3
billylanchantin
LOC: 11
defmodule Aoc2024.Day07 do
def part1(file), do: main(file, [&+/2, &*/2])
def part2(file), do: main(file, [&+/2, &*/2, &concat/2])
def main(file, fs), do: file |> f2ls(&p/1) |> Enum.reduce(0, &(&2 + value(&1, fs)))
def f2ls(f, fun), do: f |> File.read!() |> String.trim() |> String.split("\n") |> Enum.map(fun)
def p(l), do: l |> String.replace(":", "") |> String.split() |> Enum.map(&String.to_integer/1)
def value([out, in1 | rest], fs), do: if(search?(out, in1, rest, fs), do: out, else: 0)
def search?(out, acc, [], _), do: out == acc
def search?(out, acc, [h | t], fs), do: Enum.any?(fs, &search?(out, &1.(acc, h), t, fs))
def concat(a, b), do: Integer.undigits(Integer.digits(a) ++ Integer.digits(b))
end
In my actual solution, f2ls and p are better named via an import:
import Aoc2024.Util
#...
def main(file, fs), do: file |> file_to_lines(&parse/1) |> Enum.reduce(0, &(&2 + value(&1, fs)))
def parse(line), do: line |> String.replace(":", "") |> line_to_list_of_ints
But that bumps up the line count.
3
Popular in Challenges
Here is my solution for day 2 of Advent of Code:
https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
This topic is about Day 16 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Continuation of Advent of Code 2022:christmas_tree:, Day 1:
Day 2!
Leaderboard:
New
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
This was way too easy after the last few days. Simple map, filter and count.
https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
New
This topic is about Day 1 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adven...
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Hello everyone,
I’m a new in elexir and functional language. I’m trying to implement Websocket interraction with server.
On first layer...
New
Other popular topics
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
I would like to know what is the best IDE for elixir development?
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








