RocketRider & Enotstvo
I’ve been tweaking a flight‑path algorithm for a new racing game—want to see if it can outmaneuver a seasoned pilot like you?
Bring it on, show me what you’ve got, and see if you can outmaneuver a seasoned pilot like me.
Here’s a quick puzzle for you: I wrote a function that takes a list of waypoints and returns the shortest path that hits every waypoint exactly once, but the waypoints are arranged in a circle so the start point can be any of them. The code uses a simple dynamic programming approach. Give it a shot and see if you can beat the time it takes to compute it for a 12‑point circle.
```python
import math
from functools import lru_cache
def shortest_circle_path(points):
n = len(points)
dist = lambda i, j: math.dist(points[i], points[j])
@lru_cache(None)
def dp(mask, last):
if mask == (1 << n) - 1:
return 0
best = float('inf')
for nxt in range(n):
if not mask & (1 << nxt):
best = min(best, dist(last, nxt) + dp(mask | (1 << nxt), nxt))
return best
return min(dp(1 << start, start) for start in range(n))
```
Try it on a random 12‑point set and see how fast you can get it running. Good luck.
Yo, that DP is solid, but 12 points means 2¹² states, so you’re looking at a few thousand calls—should finish in a blink on a decent CPU. If you want to shave off a bit, pre‑compute the whole distance matrix so you don’t hit the math.dist each time, and switch the mask loop to iterate only over unset bits with a bit trick. That’ll cut the constant factor and you’ll see the solve time drop. Give it a spin and let me know if it’s faster than the old 10‑second benchmark!
Thanks for the tip. I pre‑computed the matrix and used the bit trick. On my laptop the 12‑point test now finishes in about 0.4 seconds, down from 10 seconds. Looks like the constant factor really mattered. Good catch.
Nice one—0.4 seconds is a killer. Time to crank it up to 15 or 20 points and see if your laptop can still keep the pace. Don’t forget memoizing the mask loop and maybe a little pruning if the branching gets out of hand. Keep it hot!