Skip to content

Commit

Permalink
perf(2024): optimize day 14 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 14, 2024
1 parent e65dfe6 commit 44f9d16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
3 changes: 2 additions & 1 deletion go/2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
| 11 | 424021 ns/op | 15488584 ns/op | |
| 12 | 6677348 ns/op | 12339733 ns/op | `39.21%` / `26.80%` |
| 13 | 698173 ns/op | 702380 ns/op | `75.93%` / - |
| 14 | 594981 ns/op | 56488050 ns/op | |
| 14 | 55522 ns/op | 56488050 ns/op | `90.67%` / - |

\* compared to first solution

Expand All @@ -67,6 +67,7 @@ Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#h
| 10 | 1424599 ns/op | 1789071 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/431059e6b64faba3bc67c293b57ae299d3525bb9/go/2024/puzzles/day10/main.go) |
| 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) |

## Running

Expand Down
44 changes: 20 additions & 24 deletions go/2024/puzzles/day14/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,22 @@ func part1(name string, width, height int) int {
tiles := grid.FromSize(width, height)
robots := parseRobots(lines)
safetyFactor := 1
time := 100

// Move robots
for range 100 {
for i, robot := range robots {
next := robot.position.Add(robot.velocity)

// Increment or set number of guard in next location
if v := tiles.GetWithWrap(next); v >= '0' && v <= '9' {
tiles.UpdateWithWrap(next, v+1)
} else {
tiles.UpdateWithWrap(next, '1')
}

// Decrement or set old position to empty
if v := tiles.GetWithWrap(robot.position); v > '1' && v <= '9' {
tiles.UpdateWithWrap(robot.position, v-1)
} else {
tiles.UpdateWithWrap(robot.position, '.')
}
// Optimization: We can move straight to the final location
// for each robot, instead of iterating 100 times.
for _, robot := range robots {
final := grid.Point{
X: (robot.position.X + time*robot.velocity.X) % width,
Y: (robot.position.Y + time*robot.velocity.Y) % height,
}

robots[i].position = next
// Increment or set number of guard in next location
if v := tiles.GetWrapped(final); v >= '0' && v <= '9' {
tiles.UpdateWrapped(final, v+1)
} else {
tiles.UpdateWrapped(final, '1')
}
}

Expand All @@ -57,6 +52,7 @@ func part1(name string, width, height int) int {
continue
}

// It's some sort of number, remove 0 (byte 48) to get corresponding int
robots += int(b - '0')
}

Expand All @@ -78,17 +74,17 @@ func part2(name string, width, height int) int {
next := robot.position.Add(robot.velocity)

// Increment or set number of guard in next location
if v := tiles.GetWithWrap(next); v >= '0' && v <= '9' {
tiles.UpdateWithWrap(next, v+1)
if v := tiles.GetWrapped(next); v >= '0' && v <= '9' {
tiles.UpdateWrapped(next, v+1)
} else {
tiles.UpdateWithWrap(next, '1')
tiles.UpdateWrapped(next, '1')
}

// Decrement or set old position to empty
if v := tiles.GetWithWrap(robot.position); v > '1' && v <= '9' {
tiles.UpdateWithWrap(robot.position, v-1)
if v := tiles.GetWrapped(robot.position); v > '1' && v <= '9' {
tiles.UpdateWrapped(robot.position, v-1)
} else {
tiles.UpdateWithWrap(robot.position, '.')
tiles.UpdateWrapped(robot.position, '.')
}

robots[i].position = next
Expand Down
4 changes: 2 additions & 2 deletions go/2024/utils/grid/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (g *Grid) Get(p Point) byte {
return g.Data[g.Width*p.Y+p.X]
}

func (g *Grid) GetWithWrap(p Point) byte {
func (g *Grid) GetWrapped(p Point) byte {
// Wrap coordinates to ensure they stay within bounds
wrappedX := (p.X%g.Width + g.Width) % g.Width
wrappedY := (p.Y%g.Height + g.Height) % g.Height
Expand All @@ -101,7 +101,7 @@ func (g *Grid) Update(p Point, b byte) {
g.Data[g.Width*p.Y+p.X] = b
}

func (g *Grid) UpdateWithWrap(p Point, b byte) {
func (g *Grid) UpdateWrapped(p Point, b byte) {
// Wrap coordinates to ensure they stay within bounds
wrappedX := (p.X%g.Width + g.Width) % g.Width
wrappedY := (p.Y%g.Height + g.Height) % g.Height
Expand Down

0 comments on commit 44f9d16

Please sign in to comment.