mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
49 lines
722 B
Go
49 lines
722 B
Go
package one
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
)
|
|
|
|
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 (
|
|
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 := intsFromString(scanner.Text())
|
|
|
|
if check(nums, cmpIncrease) || check(nums, cmpDecrease) {
|
|
sum++
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return sum, nil
|
|
}
|