29 lines
546 B
Elixir
29 lines
546 B
Elixir
defmodule Aoc2023.Day9 do
|
|
use Aoc2023
|
|
|
|
defp prediction(lst) do
|
|
case lst |> Enum.uniq() do
|
|
[val] -> val
|
|
_ -> List.last(lst) + prediction(Enum.zip_with(lst, Enum.drop(lst, 1), &(&2 - &1)))
|
|
end
|
|
end
|
|
|
|
def parse(input) do
|
|
input
|
|
|> lines
|
|
|> Enum.map(pipe(String.split() |> Enum.map(&String.to_integer/1)))
|
|
end
|
|
|
|
def part1(input) do
|
|
input
|
|
|> Enum.map(&prediction/1)
|
|
|> Enum.sum()
|
|
end
|
|
|
|
def part2(input) do
|
|
input
|
|
|> Enum.map(pipe(Enum.reverse() |> prediction))
|
|
|> Enum.sum()
|
|
end
|
|
end
|