aoc2024/day-02/internal/two/check.go

33 lines
705 B
Go
Raw Normal View History

2024-12-02 19:28:55 +00:00
package two
import (
"math"
"slices"
)
func withComparator(nums []int, comparator comparator, count int) bool {
2024-12-02 19:28:55 +00:00
if count > 1 {
return false
}
for i := 1; i < len(nums); i++ {
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
count++
removeFirst, removeSecond := nextLevels(nums, i)
return withComparator(removeFirst, comparator, count) ||
withComparator(removeSecond, comparator, count)
2024-12-02 19:28:55 +00:00
}
}
return true
}
func isSafe(m, n int) bool {
return int(math.Abs(float64(m-n))) <= 3
}
func nextLevels(nums []int, i int) ([]int, []int) {
first, second := slices.Clone(nums), slices.Clone(nums)
return append(first[:i-1], first[i:]...), append(second[:i], second[i+1:]...)
}