From 28a7a098e38713f09af56849c7025586623c176f Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Sat, 9 Dec 2023 13:37:06 -0500 Subject: [PATCH] solve 2023 9.2 --- 2023/lib/2023/9.ex | 14 +++++++++++++- README.md | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) 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