From 417266c83832e5651fc7bf449081c0afbd790d63 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 2 Dec 2024 22:48:19 +0000 Subject: [PATCH] move comparator functions into util --- day-02/internal/one/solve.go | 9 +-------- day-02/internal/two/solve.go | 9 +-------- day-02/internal/util/util.go | 8 ++++++++ 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/day-02/internal/one/solve.go b/day-02/internal/one/solve.go index 398429f..e7cbbc9 100644 --- a/day-02/internal/one/solve.go +++ b/day-02/internal/one/solve.go @@ -24,14 +24,7 @@ func parseLines(r io.Reader) int { for scanner.Scan() { nums := util.IntsFromString(scanner.Text()) - cmpIncrease := func(nums []int, i int) bool { - return nums[i-1] >= nums[i] - } - cmpDecrease := func(nums []int, i int) bool { - return nums[i-1] <= nums[i] - } - - if check(nums, cmpIncrease) || check(nums, cmpDecrease) { + if check(nums, util.CmpIncrease) || check(nums, util.CmpDecrease) { sum++ } } diff --git a/day-02/internal/two/solve.go b/day-02/internal/two/solve.go index e920012..0c6df9d 100644 --- a/day-02/internal/two/solve.go +++ b/day-02/internal/two/solve.go @@ -25,14 +25,7 @@ func parseLines(r io.Reader) int { for scanner.Scan() { nums := util.IntsFromString(scanner.Text()) - cmpIncrease := func(nums []int, i int) bool { - return nums[i-1] >= nums[i] - } - cmpDecrease := func(nums []int, i int) bool { - return nums[i-1] <= nums[i] - } - - if check(withDampener, nums, cmpIncrease) || check(withDampener, nums, cmpDecrease) { + if check(withDampener, nums, util.CmpIncrease) || check(withDampener, nums, util.CmpDecrease) { sum++ } } diff --git a/day-02/internal/util/util.go b/day-02/internal/util/util.go index 2bb9743..81de9c0 100644 --- a/day-02/internal/util/util.go +++ b/day-02/internal/util/util.go @@ -20,3 +20,11 @@ func IntsFromString(s string) []int { } return nums } + +var CmpIncrease = func(nums []int, i int) bool { + return nums[i-1] >= nums[i] +} + +var CmpDecrease = func(nums []int, i int) bool { + return nums[i-1] <= nums[i] +}