diff --git a/day-02/internal/one/check.go b/day-02/internal/one/check.go index d645234..06707b0 100644 --- a/day-02/internal/one/check.go +++ b/day-02/internal/one/check.go @@ -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 diff --git a/day-02/internal/one/check_internal_test.go b/day-02/internal/one/check_internal_test.go index b4eb969..34406c0 100644 --- a/day-02/internal/one/check_internal_test.go +++ b/day-02/internal/one/check_internal_test.go @@ -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) }) } diff --git a/day-02/internal/one/solve.go b/day-02/internal/one/solve.go index 8de2461..398429f 100644 --- a/day-02/internal/one/solve.go +++ b/day-02/internal/one/solve.go @@ -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) -} diff --git a/day-02/internal/two/check.go b/day-02/internal/two/check.go index 1aced4e..9ac98d3 100644 --- a/day-02/internal/two/check.go +++ b/day-02/internal/two/check.go @@ -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) } } diff --git a/day-02/internal/two/check_internal_test.go b/day-02/internal/two/check_internal_test.go index f32b8a2..8896a00 100644 --- a/day-02/internal/two/check_internal_test.go +++ b/day-02/internal/two/check_internal_test.go @@ -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) }) } diff --git a/day-02/internal/two/solve.go b/day-02/internal/two/solve.go index 72c06ef..f6b196d 100644 --- a/day-02/internal/two/solve.go +++ b/day-02/internal/two/solve.go @@ -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++ } }