package two import ( "bufio" "bytes" "io" ) type comparator func([]int, int) bool type dampener func([]int, comparator, 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(withDampener, nums, cmpIncrease) || check(withDampener, nums, cmpDecrease) { sum++ } } if err := scanner.Err(); err != nil { return 0, err } return sum, nil } func check(fn dampener, nums []int, cmp comparator) bool { var count int return fn(nums, cmp, count) }