2024-12-02 19:28:55 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type comparator func([]int, int) bool
|
2024-12-02 22:39:56 +00:00
|
|
|
type dampener func([]int, comparator, int) bool
|
2024-12-02 19:28:55 +00:00
|
|
|
|
|
|
|
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())
|
2024-12-02 22:28:14 +00:00
|
|
|
|
2024-12-02 22:48:19 +00:00
|
|
|
if check(withDampener, nums, util.CmpIncrease) || check(withDampener, nums, util.CmpDecrease) {
|
2024-12-02 19:28:55 +00:00
|
|
|
sum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2024-12-02 22:39:56 +00:00
|
|
|
func check(fn dampener, nums []int, cmp comparator) bool {
|
2024-12-02 19:28:55 +00:00
|
|
|
var count int
|
2024-12-02 22:39:56 +00:00
|
|
|
return fn(nums, cmp, count)
|
2024-12-02 19:28:55 +00:00
|
|
|
}
|