From 4416dab08040a6f19699f58fda545884df598f49 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 2 Dec 2024 22:39:56 +0000 Subject: [PATCH] rename withComparator to withDampener. We're using comparators regardless... add type definition dampener --- day-02/internal/one/check.go | 4 ++-- day-02/internal/two/check.go | 8 ++++---- day-02/internal/two/check_internal_test.go | 4 ++-- day-02/internal/two/solve.go | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/day-02/internal/one/check.go b/day-02/internal/one/check.go index 06707b0..945e7a4 100644 --- a/day-02/internal/one/check.go +++ b/day-02/internal/one/check.go @@ -2,9 +2,9 @@ package one import "math" -func check(nums []int, comparator comparator) bool { +func check(nums []int, cmp comparator) bool { 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 } } diff --git a/day-02/internal/two/check.go b/day-02/internal/two/check.go index 9ac98d3..6be86d2 100644 --- a/day-02/internal/two/check.go +++ b/day-02/internal/two/check.go @@ -5,17 +5,17 @@ import ( "slices" ) -func withComparator(nums []int, comparator comparator, count int) bool { +func withDampener(nums []int, cmp comparator, count int) bool { if count > 1 { return false } 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++ removeFirst, removeSecond := nextLevels(nums, i) - return withComparator(removeFirst, comparator, count) || - withComparator(removeSecond, comparator, count) + return withDampener(removeFirst, cmp, count) || + withDampener(removeSecond, cmp, count) } } diff --git a/day-02/internal/two/check_internal_test.go b/day-02/internal/two/check_internal_test.go index 8896a00..c2c9339 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 := withComparator(tc.input, cmp, 0) + got := withDampener(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 := withComparator(tc.input, cmp, 0) + got := withDampener(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 f6b196d..e920012 100644 --- a/day-02/internal/two/solve.go +++ b/day-02/internal/two/solve.go @@ -9,6 +9,7 @@ import ( ) type comparator func([]int, int) bool +type dampener func([]int, comparator, int) bool func Solve(data []byte) (int, error) { r := bytes.NewReader(data) @@ -31,7 +32,7 @@ func parseLines(r io.Reader) int { 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++ } } @@ -39,7 +40,7 @@ func parseLines(r io.Reader) int { 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 - return fn(nums, comparator, count) + return fn(nums, cmp, count) }