bjorng
Erlang Core Team
Advent of Code 2023 - Day 9
Here is my solution:
https://github.com/bjorng/advent-of-code-2023/blob/main/day09/lib/day09.ex
Most Liked
Aetherus
Share my failure on Part 1 ![]()
I was trying polynomial regression, and it explodes ![]()
The regression function for working out the coefficients of the polynomial:
# The param `y` is a list of numbers.
regression = fn y ->
y =
y
|> Nx.tensor(type: :f64)
|> Nx.new_axis(0)
|> Nx.transpose()
{n, 1} = Nx.shape(y)
v =
0..(n - 1)
|> Enum.to_list()
|> Nx.tensor(type: :f64)
x =
for p <- 0..(n-1) do
Nx.pow(v, p)
end
|> Nx.stack()
|> Nx.transpose()
xt = Nx.transpose(x)
coeffs =
xt
|> Nx.dot(x)
|> Nx.LinAlg.invert()
|> Nx.dot(xt)
|> Nx.dot(y)
errors = Nx.subtract(y, Nx.dot(x, coeffs))
{coeffs, errors}
end
And the function for predicting the last value of a line:
# x is the index of the yet-to-be-solved number in a line.
predict = fn x, {coeffs, errors} ->
{n, 1} = Nx.shape(coeffs)
x =
0..(n - 1)
|> Enum.map(& x ** &1)
|> Nx.tensor()
|> Nx.new_axis(0)
x
|> Nx.dot(coeffs)
|> Nx.add(errors)
|> Nx.mean()
|> Nx.to_number()
|> round()
end
3
lbm364dl
Not sure if unfold was the best choice for this use case but it ended up quite clean.
solve = fn first? ->
File.stream!("input.txt")
|> Stream.map(fn ln ->
String.split(ln)
|> Enum.map(&String.to_integer/1)
|> Stream.unfold(fn l ->
case Enum.all?(l, & &1 == 0) do
true -> nil
false -> {Enum.at(l, first? && 0 || -1), Enum.zip_with(tl(l), l, &-/2)}
end
end)
|> then(fn ends ->
case first? do
true -> ends |> Enum.reverse() |> Enum.reduce(&-/2)
false -> ends |> Enum.sum()
end
end)
end)
|> Enum.sum()
end
IO.inspect(solve.(false), label: "Star 1")
IO.inspect(solve.(true), label: "Star 2")
3
hauleth
Just the core:
defmodule Day09 do
def reduce(list, field \\ &List.last/1) do
reduce(list, [field.(list)], field)
end
defp reduce(list, acc, field) do
if Enum.all?(list, &(&1 == 0)) do
acc
else
new =
list
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [a, b] -> b - a end)
reduce(new, [field.(new) | acc], field)
end
end
end
Part 1:
lines
|> Enum.map(fn line ->
Day09.reduce(line)
|> Enum.reduce(&+/2)
end)
|> Enum.sum()
Part 2:
lines
|> Enum.map(fn line ->
Day09.reduce(line, &List.first/1)
|> Enum.reduce(&-/2)
end)
|> Enum.sum()
2
Popular in Challenges
It is that time of the year again: Advent of Code 2022 :christmas_tree:
Day 1
Leaderboard:
New
I have 2 arrays: a1 can be any combination of value or nil like that
a1 = [1,nil,3]
and array 2 the same
a2 = [4,2, nil]
How do I com...
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
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
Note: This topic is to talk about Day 25 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
Nobody’s doing Advent of Code this year? :grinning_face_with_smiling_eyes:
I might do the first week or so.
For Day 1, first I solved i...
New
This topic is about Day 16 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Just did part 1. Part 2 seems to be demanding too much of my reading time so will get to that after I am done with some chores.
Oh here ...
New
This topic is about Day 7 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Thought I’d kick today’s thread off!
Parsing
Enum rocks, so most of my code was actually in parsing input.
▶ Preprocessing input
Part 1...
New
Other popular topics
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I would like to know what is the best IDE for elixir development?
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









