remove the other cases where we don't need to walk the string
This commit is contained in:
parent
cfab2ff338
commit
d83b8aacec
33
02/main.go
33
02/main.go
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -83,12 +82,22 @@ func partOne() {
|
||||
line := scanner.Text()
|
||||
ok := true
|
||||
round++
|
||||
_, draws, _ := strings.Cut(line, ": ")
|
||||
for _, draws := range strings.Split(draws, "; ") {
|
||||
g := parseRound(draws)
|
||||
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 {
|
||||
sum += round
|
||||
@ -103,10 +112,18 @@ 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)
|
||||
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
|
||||
}
|
||||
@ -116,6 +133,8 @@ func partTwo() {
|
||||
if g.green > max.green {
|
||||
max.green = g.green
|
||||
}
|
||||
start = i + 2
|
||||
}
|
||||
}
|
||||
sum += max.blue * max.green * max.red
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user