From f3b957628eeb700fc7ace18c2c39c8d06ac688ee Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sat, 9 Dec 2023 13:33:54 -0500 Subject: [PATCH] Add solution for day 9 part 2 --- day9/main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/day9/main.go b/day9/main.go index 7205a25..83c2100 100644 --- a/day9/main.go +++ b/day9/main.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "slices" "strconv" "strings" ) @@ -35,6 +36,7 @@ func main() { } fmt.Printf("Part 1: %d\n", part1(histories)) + fmt.Printf("Part 2: %d\n", part2(histories)) } func part1(histories [][]int) int { @@ -46,6 +48,17 @@ func part1(histories [][]int) int { return total } +func part2(histories [][]int) int { + total := 0 + for _, history := range histories { + reversedHistory := slices.Clone(history) + slices.Reverse(reversedHistory) + total += predictNextValue(reversedHistory) + } + + return total +} + // predictNextValue calculates the next item in the sequence by using the nth differences func predictNextValue(history []int) int { if allEqual(history, 0) {