mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
41 lines
764 B
Go
41 lines
764 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(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())
|
||
|
if check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||
|
return nums[i-1] >= nums[i]
|
||
|
}) || check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||
|
return nums[i-1] <= nums[i]
|
||
|
}) {
|
||
|
sum++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return sum
|
||
|
}
|
||
|
|
||
|
func check(fn func([]int, comparator) bool, nums []int, comparator comparator) bool {
|
||
|
return fn(nums, comparator)
|
||
|
}
|