Skip to content

Commit 11a7fc6

Browse files
committed
Add FieldsIter as more optimal strings.Fields
1 parent 07441b4 commit 11a7fc6

File tree

3 files changed

+64
-39
lines changed

3 files changed

+64
-39
lines changed

day04/day04.go

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
"time"
78

89
"github.com/aaronbee/aoc2023"
910
)
@@ -18,22 +19,27 @@ func main() {
1819
for i := range counts {
1920
counts[i] = 1
2021
}
22+
t := time.Now()
2123
var part1 int
2224
for n, line := range lines {
23-
part1 += evalGame1(line)
25+
m := matches(line)
26+
if m == 0 {
27+
continue
28+
}
29+
part1 += 1 << (m - 1)
2430

25-
score := score2(line)
2631
count := counts[n]
27-
for i := 0; i < score && n+i+1 < len(counts); i++ {
32+
for i := 0; i < m && n+i+1 < len(counts); i++ {
2833
counts[n+i+1] += count
2934
}
3035
}
3136
part2 := aoc2023.SumSlice(counts)
37+
fmt.Println("time:", time.Since(t))
3238
fmt.Println("Part 1:", part1)
3339
fmt.Println("Part 2:", part2)
3440
}
3541

36-
func evalGame1(s string) int {
42+
func matches(s string) int {
3743
_, card, ok := strings.Cut(s, ": ")
3844
if !ok {
3945
panic(fmt.Errorf("unexpected line: %q", s))
@@ -42,41 +48,16 @@ func evalGame1(s string) int {
4248
if !ok {
4349
panic(fmt.Errorf("unexpected card: %q", card))
4450
}
45-
w := make(map[string]struct{})
46-
for _, n := range strings.Fields(winners) {
47-
w[n] = struct{}{}
48-
}
49-
score := 0
50-
for _, n := range strings.Fields(plays) {
51-
if _, ok := w[n]; ok {
52-
if score == 0 {
53-
score = 1
54-
} else {
55-
score <<= 1
56-
}
51+
boolSet := [101]bool{}
52+
aoc2023.FieldsIter(winners, func(s string) {
53+
boolSet[aoc2023.Atoi(s)] = true
54+
})
55+
var count int
56+
aoc2023.FieldsIter(plays, func(s string) {
57+
if boolSet[aoc2023.Atoi(s)] {
58+
count++
5759
}
58-
}
59-
return score
60-
}
60+
})
6161

62-
func score2(s string) int {
63-
_, card, ok := strings.Cut(s, ": ")
64-
if !ok {
65-
panic(fmt.Errorf("unexpected line: %q", s))
66-
}
67-
winners, plays, ok := strings.Cut(card, " | ")
68-
if !ok {
69-
panic(fmt.Errorf("unexpected card: %q", card))
70-
}
71-
w := make(map[string]struct{})
72-
for _, n := range strings.Fields(winners) {
73-
w[n] = struct{}{}
74-
}
75-
score := 0
76-
for _, n := range strings.Fields(plays) {
77-
if _, ok := w[n]; ok {
78-
score++
79-
}
80-
}
81-
return score
62+
return count
8263
}

utils.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package aoc2023
22

3+
import "strconv"
4+
35
type addable interface {
46
~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64 | ~int | ~uint
57
}
@@ -25,3 +27,26 @@ func ProdMapVal[M ~map[K]V, K comparable, V addable](m M) (prod V) {
2527
}
2628
return prod
2729
}
30+
31+
func Atoi(s string) int {
32+
i, err := strconv.Atoi(s)
33+
if err != nil {
34+
panic(err)
35+
}
36+
return i
37+
}
38+
39+
func FieldsIter(s string, fn func(s string)) {
40+
i := 0
41+
for {
42+
for ; i < len(s) && s[i] == ' '; i++ {
43+
}
44+
beg := i
45+
for ; i < len(s) && s[i] != ' '; i++ {
46+
}
47+
if beg == i {
48+
return
49+
}
50+
fn(s[beg:i])
51+
}
52+
}

utils_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package aoc2023
2+
3+
import (
4+
"slices"
5+
"testing"
6+
)
7+
8+
func TestFieldsIter(t *testing.T) {
9+
var ss []string
10+
join := func(s string) { ss = append(ss, s) }
11+
FieldsIter("0", join)
12+
if !slices.Equal(ss, []string{"0"}) {
13+
t.Error(ss)
14+
}
15+
FieldsIter(" 1 2 3 4 56 ", join)
16+
if !slices.Equal(ss, []string{"0", "1", "2", "3", "4", "56"}) {
17+
t.Error(ss)
18+
}
19+
}

0 commit comments

Comments
 (0)