You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
583 B

package day09
import (
"sort"
)
func isSumOfPreviousNumbers(current int, previous []int) bool {
for _, n1 := range previous {
for _, n2 := range previous {
if n1+n2 == current {
return true
}
}
}
return false
}
func findAnomalySum(anomaly int, numbers []int) int {
for k1 := range numbers {
sum := 0
for k2, n2 := range numbers[k1:] {
sum += n2
if sum > anomaly {
break
}
if sum == anomaly {
targetSet := numbers[k1 : k1+k2+1]
sort.Ints(targetSet)
return targetSet[0] + targetSet[len(targetSet)-1]
}
}
}
return 0
}