Day 9
This commit is contained in:
35
lib/day9.ex
Normal file
35
lib/day9.ex
Normal file
@@ -0,0 +1,35 @@
|
||||
defmodule Aoc2023.Day9 do
|
||||
use Aoc2023
|
||||
|
||||
defp differences(lst) do
|
||||
Enum.zip_with(lst, Enum.drop(lst, 1), &(&2 - &1))
|
||||
end
|
||||
|
||||
defp prediction(lst) do
|
||||
diffs = differences(lst)
|
||||
|
||||
if Enum.all?(diffs, &(&1 == 0)) do
|
||||
List.first(lst)
|
||||
else
|
||||
List.last(lst) + prediction(diffs)
|
||||
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
|
||||
Reference in New Issue
Block a user