Files
aoc-2023/lib/day9.ex
bluepython508 1655ead021 Day 9
2023-12-09 12:13:46 +00:00

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