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