remove the other cases where we don't need to walk the string

This commit is contained in:
David 2023-12-02 16:29:31 -05:00
parent cfab2ff338
commit d83b8aacec

View File

@ -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