From a6295375aa0307b8b22ddd5d7125bf1d6dd79690 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 7 Jan 2025 17:19:24 +0000 Subject: [PATCH] remove internal/util move cmpIncrease, cmpDecrease into parseLines --- day-02/internal/one/solve.go | 16 ++++++++++++---- day-02/internal/one/util.go | 22 ++++++++++++++++++++++ day-02/internal/two/solve.go | 16 ++++++++++++---- day-02/internal/two/util.go | 22 ++++++++++++++++++++++ day-02/internal/util/util.go | 30 ------------------------------ 5 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 day-02/internal/one/util.go create mode 100644 day-02/internal/two/util.go delete mode 100644 day-02/internal/util/util.go diff --git a/day-02/internal/one/solve.go b/day-02/internal/one/solve.go index 118a0ef..6c88362 100644 --- a/day-02/internal/one/solve.go +++ b/day-02/internal/one/solve.go @@ -4,8 +4,6 @@ import ( "bufio" "bytes" "io" - - "github.com/onyx-and-iris/aoc2024/day-02/internal/util" ) type comparator func([]int, int) bool @@ -21,13 +19,23 @@ func Solve(buf []byte) (int, error) { } func parseLines(r io.Reader) (int, error) { + var ( + 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] + } + ) + var sum int scanner := bufio.NewScanner(r) for scanner.Scan() { - nums := util.IntsFromString(scanner.Text()) + nums := intsFromString(scanner.Text()) - if check(nums, util.CmpIncrease) || check(nums, util.CmpDecrease) { + if check(nums, cmpIncrease) || check(nums, cmpDecrease) { sum++ } } diff --git a/day-02/internal/one/util.go b/day-02/internal/one/util.go new file mode 100644 index 0000000..67e4219 --- /dev/null +++ b/day-02/internal/one/util.go @@ -0,0 +1,22 @@ +package one + +import ( + "strconv" + "strings" +) + +func mustConv(s string) int { + n, err := strconv.Atoi(s) + if err != nil { + panic(err) + } + return n +} + +func intsFromString(s string) []int { + nums := []int{} + for _, r := range strings.Fields(s) { + nums = append(nums, mustConv(r)) + } + return nums +} diff --git a/day-02/internal/two/solve.go b/day-02/internal/two/solve.go index 6d06844..3c36662 100644 --- a/day-02/internal/two/solve.go +++ b/day-02/internal/two/solve.go @@ -4,8 +4,6 @@ import ( "bufio" "bytes" "io" - - "github.com/onyx-and-iris/aoc2024/day-02/internal/util" ) type comparator func([]int, int) bool @@ -22,13 +20,23 @@ func Solve(buf []byte) (int, error) { } func parseLines(r io.Reader) (int, error) { + var ( + 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] + } + ) + var sum int scanner := bufio.NewScanner(r) for scanner.Scan() { - nums := util.IntsFromString(scanner.Text()) + nums := intsFromString(scanner.Text()) - if check(withDampener, nums, util.CmpIncrease) || check(withDampener, nums, util.CmpDecrease) { + if check(withDampener, nums, cmpIncrease) || check(withDampener, nums, cmpDecrease) { sum++ } } diff --git a/day-02/internal/two/util.go b/day-02/internal/two/util.go new file mode 100644 index 0000000..9da26a4 --- /dev/null +++ b/day-02/internal/two/util.go @@ -0,0 +1,22 @@ +package two + +import ( + "strconv" + "strings" +) + +func mustConv(s string) int { + n, err := strconv.Atoi(s) + if err != nil { + panic(err) + } + return n +} + +func intsFromString(s string) []int { + nums := []int{} + for _, r := range strings.Fields(s) { + nums = append(nums, mustConv(r)) + } + return nums +} diff --git a/day-02/internal/util/util.go b/day-02/internal/util/util.go deleted file mode 100644 index 81de9c0..0000000 --- a/day-02/internal/util/util.go +++ /dev/null @@ -1,30 +0,0 @@ -package util - -import ( - "strconv" - "strings" -) - -func MustConv(s string) int { - n, err := strconv.Atoi(s) - if err != nil { - panic(err) - } - return n -} - -func IntsFromString(s string) []int { - nums := []int{} - for _, r := range strings.Fields(s) { - nums = append(nums, MustConv(r)) - } - 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] -}