return scanner errors

This commit is contained in:
onyx-and-iris 2024-12-03 02:29:28 +00:00
parent 417266c838
commit 64f4df4fea
2 changed files with 20 additions and 6 deletions

View File

@ -12,12 +12,15 @@ type comparator func([]int, int) bool
func Solve(data []byte) (int, error) {
r := bytes.NewReader(data)
sum := parseLines(r)
sum, err := parseLines(r)
if err != nil {
return 0, err
}
return sum, nil
}
func parseLines(r io.Reader) int {
func parseLines(r io.Reader) (int, error) {
var sum int
scanner := bufio.NewScanner(r)
@ -29,5 +32,9 @@ func parseLines(r io.Reader) int {
}
}
return sum
if err := scanner.Err(); err != nil {
return 0, err
}
return sum, nil
}

View File

@ -13,12 +13,15 @@ type dampener func([]int, comparator, int) bool
func Solve(data []byte) (int, error) {
r := bytes.NewReader(data)
sum := parseLines(r)
sum, err := parseLines(r)
if err != nil {
return 0, err
}
return sum, nil
}
func parseLines(r io.Reader) int {
func parseLines(r io.Reader) (int, error) {
var sum int
scanner := bufio.NewScanner(r)
@ -30,7 +33,11 @@ func parseLines(r io.Reader) int {
}
}
return sum
if err := scanner.Err(); err != nil {
return 0, err
}
return sum, nil
}
func check(fn dampener, nums []int, cmp comparator) bool {