mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
assign comparator functions to variables for readability
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
func checkWithComparator(nums []int, comparator comparator, count int) bool {
|
||||
func withComparator(nums []int, comparator comparator, count int) bool {
|
||||
if count > 1 {
|
||||
return false
|
||||
}
|
||||
@@ -14,8 +14,8 @@ func checkWithComparator(nums []int, comparator comparator, count int) bool {
|
||||
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
||||
count++
|
||||
removeFirst, removeSecond := nextLevels(nums, i)
|
||||
return checkWithComparator(removeFirst, comparator, count) ||
|
||||
checkWithComparator(removeSecond, comparator, count)
|
||||
return withComparator(removeFirst, comparator, count) ||
|
||||
withComparator(removeSecond, comparator, count)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestIncreasedWithDampener(t *testing.T) {
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp, 0)
|
||||
got := withComparator(tc.input, cmp, 0)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func TestDecreasedWithDampener(t *testing.T) {
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp, 0)
|
||||
got := withComparator(tc.input, cmp, 0)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,11 +23,15 @@ func parseLines(r io.Reader) int {
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
nums := util.IntsFromString(scanner.Text())
|
||||
if check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
|
||||
cmpIncrease := func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}) || check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
}
|
||||
cmpDecrease := func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}) {
|
||||
}
|
||||
|
||||
if check(withComparator, nums, cmpIncrease) || check(withComparator, nums, cmpDecrease) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user