Day 9
This commit is contained in:
28
lib/day9.ex
Normal file
28
lib/day9.ex
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
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
|
||||||
@@ -10,6 +10,7 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
defp module(6), do: Aoc2023.Day6
|
defp module(6), do: Aoc2023.Day6
|
||||||
defp module(7), do: Aoc2023.Day7
|
defp module(7), do: Aoc2023.Day7
|
||||||
defp module(8), do: Aoc2023.Day8
|
defp module(8), do: Aoc2023.Day8
|
||||||
|
defp module(9), do: Aoc2023.Day9
|
||||||
# [MODULE INSERTION POINT]
|
# [MODULE INSERTION POINT]
|
||||||
|
|
||||||
defp base_dir(), do: System.get_env("AOC_BASE")
|
defp base_dir(), do: System.get_env("AOC_BASE")
|
||||||
|
|||||||
3
tests/day9/1
Normal file
3
tests/day9/1
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
0 3 6 9 12 15
|
||||||
|
1 3 6 10 15 21
|
||||||
|
10 13 16 21 30 45
|
||||||
1
tests/day9/1.1
Normal file
1
tests/day9/1.1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
114
|
||||||
1
tests/day9/1.2
Normal file
1
tests/day9/1.2
Normal file
@@ -0,0 +1 @@
|
|||||||
|
2
|
||||||
Reference in New Issue
Block a user