mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-22 10:20:48 +00:00
26 lines
486 B
Go
26 lines
486 B
Go
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
|
|
}
|