From 5b1ca25d3f30c64efc6a4ed1fd50bba7a4702f74 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 2 Dec 2023 22:40:24 +0000 Subject: [PATCH] cut loop short if any value exceeds its limit --- day-2/one.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/day-2/one.go b/day-2/one.go index d2db6c4..0364bc2 100644 --- a/day-2/one.go +++ b/day-2/one.go @@ -21,7 +21,6 @@ var limits = map[string]int{ // isValidGame calculates if a game was possible based on the limits of each cube colour func isValidGame(data string) (bool, error) { - valid := true for _, colour := range []string{"red", "green", "blue"} { m := regex[colour].FindAllStringSubmatch(data, -1) for _, s := range m { @@ -30,12 +29,12 @@ func isValidGame(data string) (bool, error) { return false, err } if n > limits[colour] { - valid = false + return false, nil } } } - return valid, nil + return true, nil } // one returns the sum of ids for all games that were possible