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" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -83,12 +82,22 @@ func partOne() {
line := scanner.Text() line := scanner.Text()
ok := true ok := true
round++ round++
_, draws, _ := strings.Cut(line, ": ") for i, c := range line {
for _, draws := range strings.Split(draws, "; ") { if c == ':' {
g := parseRound(draws) 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 { if g.red > max.red || g.blue > max.blue || g.green > max.green {
ok = false ok = false
} }
start = i + 2
}
} }
if ok { if ok {
sum += round sum += round
@ -103,10 +112,18 @@ func partTwo() {
sum := 0 sum := 0
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
_, draws, _ := strings.Cut(line, ": ") for i, c := range line {
if c == ':' {
line = line[i+2:]
break
}
}
max := game{} max := game{}
for _, draws := range strings.Split(draws, "; ") { var start, end int
g := parseRound(draws) for i, c := range line {
if c == ';' || i == len(line)-1 {
end = i + 1
g := parseRound(line[start:end])
if g.red > max.red { if g.red > max.red {
max.red = g.red max.red = g.red
} }
@ -116,6 +133,8 @@ func partTwo() {
if g.green > max.green { if g.green > max.green {
max.green = g.green max.green = g.green
} }
start = i + 2
}
} }
sum += max.blue * max.green * max.red sum += max.blue * max.green * max.red
} }