roll my own parser for each round for a little extra speed

This commit is contained in:
David 2023-12-02 16:20:21 -05:00
parent 725e453168
commit cfab2ff338

View File

@ -40,6 +40,35 @@ type game struct {
blue int blue int
} }
func parseRound(line string) game {
g := game{}
n := []byte{}
for i := 0; i < len(line); i++ {
switch line[i] {
case ' ':
continue
case ',':
i++
continue
case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
n = append(n, line[i])
case 'r':
g.red = mustAtoi(string(n))
n = []byte{}
i += 4
case 'g':
g.green = mustAtoi(string(n))
n = []byte{}
i += 6
case 'b':
g.blue = mustAtoi(string(n))
n = []byte{}
i += 5
}
}
return g
}
func partOne() { func partOne() {
scanner := makeScanner(false) scanner := makeScanner(false)
@ -56,20 +85,7 @@ func partOne() {
round++ round++
_, draws, _ := strings.Cut(line, ": ") _, draws, _ := strings.Cut(line, ": ")
for _, draws := range strings.Split(draws, "; ") { for _, draws := range strings.Split(draws, "; ") {
iter := strings.Split(draws, " ") g := parseRound(draws)
g := game{}
for i := 0; i < len(iter); i++ {
seen := mustAtoi(iter[i])
i++
switch iter[i][0] {
case 'r':
g.red = seen
case 'g':
g.green = seen
case 'b':
g.blue = seen
}
}
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
} }
@ -90,20 +106,7 @@ func partTwo() {
_, draws, _ := strings.Cut(line, ": ") _, draws, _ := strings.Cut(line, ": ")
max := game{} max := game{}
for _, draws := range strings.Split(draws, "; ") { for _, draws := range strings.Split(draws, "; ") {
iter := strings.Split(draws, " ") g := parseRound(draws)
g := game{}
for i := 0; i < len(iter); i++ {
seen := mustAtoi(iter[i])
i++
switch iter[i][0] {
case 'r':
g.red = seen
case 'g':
g.green = seen
case 'b':
g.blue = seen
}
}
if g.red > max.red { if g.red > max.red {
max.red = g.red max.red = g.red
} }