Day9
Mix.install([
{:kino, "~> 0.11.3"}
])
Section
input = Kino.Input.textarea("Enter input here:")
defmodule Day9Part1 do
def solve(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(
&(String.split(&1, " ", trim: true)
|> Enum.map(fn x -> String.to_integer(x) end))
)
|> Enum.map(&solve_one/1)
|> Enum.sum()
end
defp solve_one(data) do
next_sequence =
Enum.zip(data, Enum.slice(data, 1..-1))
|> Enum.map(fn {a, b} -> b - a end)
num_non_zeroes = Enum.filter(next_sequence, &(&1 != 0)) |> length()
case num_non_zeroes do
0 -> List.last(data)
_ -> List.last(data) + solve_one(next_sequence)
end
end
end
{:module, Day9Part1, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:solve_one, 1}}
input
|> Kino.Input.read()
|> Day9Part1.solve()
1725987467
Part 2
defmodule Day9Part2 do
def solve(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(
&(String.split(&1, " ", trim: true)
|> Enum.map(fn x -> String.to_integer(x) end))
)
|> Enum.map(&solve_one/1)
|> Enum.sum()
end
defp solve_one(data) do
next_sequence =
Enum.zip(data, Enum.slice(data, 1..-1))
|> Enum.map(fn {a, b} -> b - a end)
num_non_zeroes = Enum.filter(next_sequence, &(&1 != 0)) |> length()
case num_non_zeroes do
0 -> hd(data)
_ -> hd(data) - solve_one(next_sequence)
end
end
end
{:module, Day9Part2, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:solve_one, 1}}
input
|> Kino.Input.read()
|> Day9Part2.solve()
971