aoc2023/day-9/two.go
2023-12-09 17:25:53 +00:00

26 lines
479 B
Go

package main
// previous returns the previous value in the sequence
func previous(nums []int) int {
diffs := []int{}
for i := 1; i < len(nums); i++ {
diffs = append(diffs, nums[i]-nums[i-1])
}
if allEqual(diffs) {
return nums[0] - diffs[0]
}
return nums[0] - previous(diffs)
}
// two returns the sum of all previous values
func two(lines []string) int {
sum := 0
for _, line := range lines {
nums := convertToInts(line)
sum += previous(nums)
}
return sum
}