bjorng
Erlang Core Team
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
midouest
Converted part 1 to a simple visualization using Kino + VegaLite:

Code
alias VegaLite, as: Vl
defmodule Viz do
def difference(widget, history), do: difference(widget, history, [])
def difference(widget, prev_diffs, rest_diffs) do
i = length(rest_diffs)
data =
for {diff, x} <- Enum.with_index(prev_diffs) do
%{"x" => x, "y" => diff, "i" => i}
end
Kino.VegaLite.push_many(widget, data)
Process.sleep(div(500, i + 1))
if Enum.all?(prev_diffs, &(&1 == 0)) do
predictr(widget, [prev_diffs | rest_diffs])
else
next_diffs =
prev_diffs
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn xs -> Enum.reduce(xs, &Kernel.-/2) end)
difference(widget, next_diffs, [prev_diffs | rest_diffs])
end
end
def predictr(widget, diffs), do: predictr(widget, diffs, 0)
def predictr(_, [], extrapolation), do: extrapolation
def predictr(widget, [diffs | rest], extrapolation) do
next_extrapolation = extrapolation + List.last(diffs)
i = length(rest)
Kino.VegaLite.push(widget, %{
"x" => length(diffs),
"y" => next_extrapolation,
"i" => i
})
Process.sleep(div(500, i + 1))
predictr(widget, rest, next_extrapolation)
end
end
widget =
Vl.new(width: 600, height: 300)
|> Vl.mark(:line)
|> Vl.encode_field(:x, "x", type: :quantitative)
|> Vl.encode_field(:y, "y", type: :quantitative)
|> Vl.encode_field(:color, "i", type: :nominal)
|> Kino.VegaLite.render()
for history <- histories do
Kino.VegaLite.clear(widget)
Viz.difference(widget, history)
end
2
trnasistor
Thank you! Turns out, I was wrong to take absolute value of the difference like so:
new_value = abs(head - List.first(tail))
Instead, I had to simply subtract the second value from the first
new_value = List.first(tail) - head
With this change, I got the right answer immediately.
2
Popular in Challenges
Note: This topic is to talk about Day 9 of the Advent of Code.
For general discussion about the Advent of Code 2018 and links to topics ...
New
Note by the Moderators: This topic is to talk about the first day of the Advent of Code.
For general discussion about the Advent of Code...
New
This topic is about Day 5 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Note: This topic is to talk about Day 2 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can joi...
New
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
Hey there :wave:
No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New








