aoc2024/day-02/internal/one/solve.go
2024-12-06 19:57:07 +00:00

41 lines
619 B
Go

package one
import (
"bufio"
"bytes"
"io"
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
)
type comparator func([]int, int) bool
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
sum, err := parseLines(r)
if err != nil {
return 0, err
}
return sum, nil
}
func parseLines(r io.Reader) (int, error) {
var sum int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
nums := util.IntsFromString(scanner.Text())
if check(nums, util.CmpIncrease) || check(nums, util.CmpDecrease) {
sum++
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
return sum, nil
}