Skip to content

Commit 403ad31

Browse files
committed
day20: wrong solution for part two
1 parent a4a39d4 commit 403ad31

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

Diff for: day20/main.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"sync"
89

910
"github.com/fxnn/adventofcode2024/util"
1011
)
@@ -151,12 +152,29 @@ func main() {
151152
var moves = findMoves(course)
152153
var time = len(moves) - 1
153154
fmt.Printf("course takes %d picoseconds\n", time)
154-
var cheats = make(map[cheat]util.Void)
155+
156+
var cheatChan = make(chan cheat)
157+
var inProgressMoves sync.WaitGroup
155158
for i := range moves {
156-
for _, c := range findCheats(course, moves, i, *minSaving, *debug) {
157-
cheats[c] = util.Void{}
158-
}
159-
fmt.Printf("searched cheats for %d/%d moves\n", i+1, len(moves))
159+
inProgressMoves.Add(1)
160+
go func(startIdx int) {
161+
defer inProgressMoves.Done()
162+
for _, c := range findCheats(course, moves, startIdx, *minSaving, *debug) {
163+
cheatChan <- c
164+
}
165+
fmt.Printf("searched cheats for %d/%d moves\n", startIdx+1, len(moves))
166+
}(i)
160167
}
168+
169+
go func() {
170+
inProgressMoves.Wait()
171+
close(cheatChan)
172+
}()
173+
174+
var cheats = make(map[cheat]util.Void)
175+
for c := range cheatChan {
176+
cheats[c] = util.Void{}
177+
}
178+
161179
fmt.Printf("found %d unique cheats saving >= %d picoseconds", len(cheats), *minSaving)
162180
}

0 commit comments

Comments
 (0)