From 64f4df4feabb55778c1fa1401e584bd1cda27878 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 3 Dec 2024 02:29:28 +0000 Subject: [PATCH] return scanner errors --- day-02/internal/one/solve.go | 13 ++++++++++--- day-02/internal/two/solve.go | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/day-02/internal/one/solve.go b/day-02/internal/one/solve.go index e7cbbc9..08f9024 100644 --- a/day-02/internal/one/solve.go +++ b/day-02/internal/one/solve.go @@ -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 } diff --git a/day-02/internal/two/solve.go b/day-02/internal/two/solve.go index 0c6df9d..61dbdc6 100644 --- a/day-02/internal/two/solve.go +++ b/day-02/internal/two/solve.go @@ -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 {