remove internal/util

move cmpIncrease, cmpDecrease into parseLines
This commit is contained in:
onyx-and-iris 2025-01-07 17:19:24 +00:00
parent aeb57b5cca
commit a6295375aa
5 changed files with 68 additions and 38 deletions

View File

@ -4,8 +4,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"io" "io"
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
) )
type comparator func([]int, int) bool type comparator func([]int, int) bool
@ -21,13 +19,23 @@ func Solve(buf []byte) (int, error) {
} }
func parseLines(r io.Reader) (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 var sum int
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { 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++ sum++
} }
} }

View File

@ -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
}

View File

@ -4,8 +4,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"io" "io"
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
) )
type comparator func([]int, int) bool type comparator func([]int, int) bool
@ -22,13 +20,23 @@ func Solve(buf []byte) (int, error) {
} }
func parseLines(r io.Reader) (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 var sum int
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { 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++ sum++
} }
} }

View File

@ -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
}

View File

@ -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]
}