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)
|
2024-12-03 02:29:28 +00:00
|
|
|
sum, err := parseLines(r)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-12-02 19:28:55 +00:00
|
|
|
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2024-12-03 02:29:28 +00:00
|
|
|
func parseLines(r io.Reader) (int, error) {
|
2024-12-02 19:28:55 +00:00
|
|
|
var sum int
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
nums := util.IntsFromString(scanner.Text())
|
2024-12-02 22:28:14 +00:00
|
|
|
|
2024-12-02 22:48:19 +00:00
|
|
|
if check(nums, util.CmpIncrease) || check(nums, util.CmpDecrease) {
|
2024-12-02 19:28:55 +00:00
|
|
|
sum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-03 02:29:28 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum, nil
|
2024-12-02 19:28:55 +00:00
|
|
|
}
|