diff --git a/02/main.go b/02/main.go index bc39615..ef69df2 100644 --- a/02/main.go +++ b/02/main.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "strconv" - "strings" "time" ) @@ -83,11 +82,21 @@ func partOne() { line := scanner.Text() ok := true round++ - _, draws, _ := strings.Cut(line, ": ") - for _, draws := range strings.Split(draws, "; ") { - g := parseRound(draws) - if g.red > max.red || g.blue > max.blue || g.green > max.green { - ok = false + for i, c := range line { + if c == ':' { + line = line[i+2:] + break + } + } + var start, end int + for i, c := range line { + if c == ';' || i == len(line)-1 { + end = i + 1 + g := parseRound(line[start:end]) + if g.red > max.red || g.blue > max.blue || g.green > max.green { + ok = false + } + start = i + 2 } } if ok { @@ -103,18 +112,28 @@ func partTwo() { sum := 0 for scanner.Scan() { line := scanner.Text() - _, draws, _ := strings.Cut(line, ": ") + for i, c := range line { + if c == ':' { + line = line[i+2:] + break + } + } max := game{} - for _, draws := range strings.Split(draws, "; ") { - g := parseRound(draws) - if g.red > max.red { - max.red = g.red - } - if g.blue > max.blue { - max.blue = g.blue - } - if g.green > max.green { - max.green = g.green + var start, end int + for i, c := range line { + if c == ';' || i == len(line)-1 { + end = i + 1 + g := parseRound(line[start:end]) + if g.red > max.red { + max.red = g.red + } + if g.blue > max.blue { + max.blue = g.blue + } + if g.green > max.green { + max.green = g.green + } + start = i + 2 } } sum += max.blue * max.green * max.red