diff --git a/2023/lib/2023/9.ex b/2023/lib/2023/9.ex index 01da81e..500c4f7 100644 --- a/2023/lib/2023/9.ex +++ b/2023/lib/2023/9.ex @@ -9,7 +9,11 @@ aoc 2023, 9 do |> Enum.sum() end - def p2(_input) do + def p2(input) do + input + |> lines_of_integers() + |> Enum.map(&prev_in_sequence/1) + |> Enum.sum() end def next_in_sequence(xs) do @@ -19,6 +23,14 @@ aoc 2023, 9 do |> Enum.sum() end + def prev_in_sequence(xs) do + xs + |> differentiate() + |> Enum.map(&List.first/1) + |> Enum.reverse() + |> Enum.reduce(&(&1 - &2)) + end + def differentiate(xs) do xs |> Stream.iterate(&deltas/1) diff --git a/README.md b/README.md index ec67642..e2f63b1 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **14/50** 🌟 +1. [2023] **15/50** 🌟 [2015]: ./2015 [2017]: ./2017