Skip to content

Commit 870070a

Browse files
committed
feat(2024): parallelize day 20 for performance
1 parent 305eba9 commit 870070a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

go/2024/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
6060
| 17 | 13964 ns/op | 561424 ns/op | |
6161
| 18 | 755414 ns/op | 1996995 ns/op | |
6262
| 19 | 16126963 ns/op | 16206756 ns/op | |
63-
| 20 | 762519479 ns/op | 845552729 ns/op | |
63+
| 20 | 762519479 ns/op | 845552729 ns/op | `85.99%` / `63.78%` |
6464

6565
\* compared to first solution
6666

@@ -80,6 +80,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
8080
| 12 | 10984420 ns/op | 16856988 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/7a220ed0e6deae74d0a293615e6348e6ce1a9a22/go/2024/puzzles/day12/main.go) |
8181
| 13 | 2900453 ns/op | 702380 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/0cf31064eb05f384cebe45cbeaf80ba90e0947ce/go/2024/puzzles/day13/main.go) |
8282
| 14 | 594981 ns/op | 56488050 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/a3f28eb2691d3e4be60ec56ab7f699332a2b3d31/go/2024/puzzles/day14/main.go) |
83+
| 20 | 762519479 ns/op | 845552729 ns/op | | [Link](https://github.com/believer/advent-of-code/blob/305eba9ced6b40ecce606cf19f7cb9fc00e5ed73/go/2024/puzzles/day20/main.go) |
8384

8485
## Running
8586

go/2024/puzzles/day20/main.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"sync"
56

67
"github.com/believer/aoc-2024/utils/files"
78
"github.com/believer/aoc-2024/utils/grid"
@@ -58,19 +59,32 @@ func race(name string, maxCheatTime int) int {
5859
}
5960
}
6061

62+
var mu sync.Mutex
63+
var wg sync.WaitGroup
64+
6165
for p := range distances {
62-
for p2 := range distances {
63-
d := p.ManhattanDistance(p2)
66+
wg.Add(1)
6467

65-
if d > maxCheatTime {
66-
continue
67-
}
68+
go func(p grid.Point) {
69+
defer wg.Done()
70+
71+
for p2 := range distances {
72+
d := p.ManhattanDistance(p2)
6873

69-
if distances[p2]-distances[p]-d >= 100 {
70-
cheats++
74+
if d > maxCheatTime {
75+
continue
76+
}
77+
78+
if distances[p2]-distances[p]-d >= 100 {
79+
mu.Lock()
80+
cheats++
81+
mu.Unlock()
82+
}
7183
}
72-
}
84+
}(p)
7385
}
7486

87+
wg.Wait()
88+
7589
return cheats
7690
}

0 commit comments

Comments
 (0)