rename withComparator to withDampener. We're using comparators regardless...

add type definition dampener
This commit is contained in:
onyx-and-iris 2024-12-02 22:39:56 +00:00
parent d321e6c977
commit 4416dab080
4 changed files with 12 additions and 11 deletions

View File

@ -2,9 +2,9 @@ package one
import "math" import "math"
func check(nums []int, comparator comparator) bool { func check(nums []int, cmp comparator) bool {
for i := 1; i < len(nums); i++ { for i := 1; i < len(nums); i++ {
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) { if cmp(nums, i) || !isSafe(nums[i-1], nums[i]) {
return false return false
} }
} }

View File

@ -5,17 +5,17 @@ import (
"slices" "slices"
) )
func withComparator(nums []int, comparator comparator, count int) bool { func withDampener(nums []int, cmp comparator, count int) bool {
if count > 1 { if count > 1 {
return false return false
} }
for i := 1; i < len(nums); i++ { for i := 1; i < len(nums); i++ {
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) { if cmp(nums, i) || !isSafe(nums[i-1], nums[i]) {
count++ count++
removeFirst, removeSecond := nextLevels(nums, i) removeFirst, removeSecond := nextLevels(nums, i)
return withComparator(removeFirst, comparator, count) || return withDampener(removeFirst, cmp, count) ||
withComparator(removeSecond, comparator, count) withDampener(removeSecond, cmp, count)
} }
} }

View File

@ -36,7 +36,7 @@ func TestIncreasedWithDampener(t *testing.T) {
for name, tc := range tt { for name, tc := range tt {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
got := withComparator(tc.input, cmp, 0) got := withDampener(tc.input, cmp, 0)
assert.Equal(t, tc.want, got) assert.Equal(t, tc.want, got)
}) })
} }
@ -54,7 +54,7 @@ func TestDecreasedWithDampener(t *testing.T) {
for name, tc := range tt { for name, tc := range tt {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
got := withComparator(tc.input, cmp, 0) got := withDampener(tc.input, cmp, 0)
assert.Equal(t, tc.want, got) assert.Equal(t, tc.want, got)
}) })
} }

View File

@ -9,6 +9,7 @@ import (
) )
type comparator func([]int, int) bool type comparator func([]int, int) bool
type dampener func([]int, comparator, int) bool
func Solve(data []byte) (int, error) { func Solve(data []byte) (int, error) {
r := bytes.NewReader(data) r := bytes.NewReader(data)
@ -31,7 +32,7 @@ func parseLines(r io.Reader) int {
return nums[i-1] <= nums[i] return nums[i-1] <= nums[i]
} }
if check(withComparator, nums, cmpIncrease) || check(withComparator, nums, cmpDecrease) { if check(withDampener, nums, cmpIncrease) || check(withDampener, nums, cmpDecrease) {
sum++ sum++
} }
} }
@ -39,7 +40,7 @@ func parseLines(r io.Reader) int {
return sum return sum
} }
func check(fn func([]int, comparator, int) bool, nums []int, comparator comparator) bool { func check(fn dampener, nums []int, cmp comparator) bool {
var count int var count int
return fn(nums, comparator, count) return fn(nums, cmp, count)
} }