aoc2023/day-9/one.go

26 lines
486 B
Go
Raw Normal View History

2023-12-09 17:25:53 +00:00
package main
// next returns the next value in the sequence
func next(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[len(nums)-1] + diffs[len(diffs)-1]
}
return nums[len(nums)-1] + next(diffs)
}
// one returns the sum of all next values
func one(lines []string) int {
sum := 0
for _, line := range lines {
nums := convertToInts(line)
sum += next(nums)
}
return sum
}