aoc2024/day-02/internal/one/solve.go

41 lines
664 B
Go
Raw Normal View History

2024-12-02 19:28:55 +00:00
package one
import (
"bufio"
"bytes"
"io"
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
)
type comparator func([]int, int) bool
func Solve(data []byte) (int, error) {
r := bytes.NewReader(data)
sum := parseLines(r)
return sum, nil
}
func parseLines(r io.Reader) int {
var sum int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
nums := util.IntsFromString(scanner.Text())
cmpIncrease := func(nums []int, i int) bool {
2024-12-02 19:28:55 +00:00
return nums[i-1] >= nums[i]
}
cmpDecrease := func(nums []int, i int) bool {
2024-12-02 19:28:55 +00:00
return nums[i-1] <= nums[i]
}
if check(nums, cmpIncrease) || check(nums, cmpDecrease) {
2024-12-02 19:28:55 +00:00
sum++
}
}
return sum
}