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:
@@ -2,7 +2,7 @@ package one
|
||||
|
||||
import "math"
|
||||
|
||||
func checkWithComparator(nums []int, comparator comparator) bool {
|
||||
func check(nums []int, comparator comparator) bool {
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
||||
return false
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestCheckIncreased(t *testing.T) {
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp)
|
||||
got := check(tc.input, cmp)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func TestCheckDecreased(t *testing.T) {
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp)
|
||||
got := check(tc.input, cmp)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,18 +23,18 @@ 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(nums, cmpIncrease) || check(nums, cmpDecrease) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func check(fn func([]int, comparator) bool, nums []int, comparator comparator) bool {
|
||||
return fn(nums, comparator)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user