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