Skip to content

Commit

Permalink
feat(2024): parallelize day 20 for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 20, 2024
1 parent 305eba9 commit 870070a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion go/2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
| 17 | 13964 ns/op | 561424 ns/op | |
| 18 | 755414 ns/op | 1996995 ns/op | |
| 19 | 16126963 ns/op | 16206756 ns/op | |
| 20 | 762519479 ns/op | 845552729 ns/op | |
| 20 | 762519479 ns/op | 845552729 ns/op | `85.99%` / `63.78%` |

\* compared to first solution

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

## Running

Expand Down
30 changes: 22 additions & 8 deletions go/2024/puzzles/day20/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"sync"

"github.com/believer/aoc-2024/utils/files"
"github.com/believer/aoc-2024/utils/grid"
Expand Down Expand Up @@ -58,19 +59,32 @@ func race(name string, maxCheatTime int) int {
}
}

var mu sync.Mutex
var wg sync.WaitGroup

for p := range distances {
for p2 := range distances {
d := p.ManhattanDistance(p2)
wg.Add(1)

if d > maxCheatTime {
continue
}
go func(p grid.Point) {
defer wg.Done()

for p2 := range distances {
d := p.ManhattanDistance(p2)

if distances[p2]-distances[p]-d >= 100 {
cheats++
if d > maxCheatTime {
continue
}

if distances[p2]-distances[p]-d >= 100 {
mu.Lock()
cheats++
mu.Unlock()
}
}
}
}(p)
}

wg.Wait()

return cheats
}

0 comments on commit 870070a

Please sign in to comment.