You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for really good concise solutions again this year. Maybe @norvig and Karpathy are the two people who know the expressive power of python the best. I come back to these solutions every year to not only check alternate approaches but to also learn writing better Python.
Since the distances are same throughout (1 unit) in Race Condition problem (Day 20), Dijkstra is overkill here although it obviously works perfectly fine. So BFS can asymptotically improve the running time.
withopen("input.txt", "r") asfile:
lines=file.read().strip().split("\n")
graph= {i+j*1j: cfori, rinenumerate(lines) forj, cinenumerate(r)}
start=next(pforpingraphifgraph[p] =="S")
end=next(pforpingraphifgraph[p] =="E")
defbfs(point, graph):
dist= {}
q= [(point, 0)]
visited= {point}
whileq:
p, d=q.pop(0)
dist[p] =dfordirin (-1, 1j, 1, -1j):
np=p+dirifgraph.get(np, "#") !="#"andnpnotinvisited:
q.append((np, d+1))
visited.add(np)
returndistdefneighborhood(point, radius):
"""All points within `radius` of `point` (using taxi distance)."""return [
point+dx+dy*1jfordxinrange(-radius, radius+1)
fordyinrange(-(radius-abs(dx)), radius-abs(dx) +1)
]
deftaxi_distance(p1, p2):
returnabs(p1.real-p2.real) +abs(p1.imag-p2.imag)
defcheats(racetrack, lower_bound=1, radius=20):
"""All ways of cheating by taking up to `radius` steps through walls and back to the track."""D=bfs(end, racetrack)
return [
(p1, p2, t)
forp1inDforp2inneighborhood(p1, radius)
ifp2inDand (t:=D[p1] -D[p2] -taxi_distance(p1, p2)) >=lower_bound
]
print(len(cheats(graph, 100, 2)))
print(len(cheats(graph, 100, 20)))
The text was updated successfully, but these errors were encountered:
Thanks for the kind note! You are absolutely right about the BFS. But I
don't think there is an asymptotically improvement to be had (although
certainly there could be constant factor improvements, say by using numpy
arrays rather than dicts.
I said that "This is an all-paths-to-the-goal puzzle, which should make you
think *Dijkstra's algorithm*
<https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm>.", and I stand by
that. But I also said "Since we are told the grid is single-path, we don't
have to worry about updating entries in distance_from for a second path to
a point." I should have also said "we don't have to worry about the order
of the points in the queue, as long as we're appending to the end." And, I
should have said "with these two simplifications, my Dijkstra becomes a
breadth-first search." But I stand by the point that when you notice it is
all-paths, you should start thinking Dijkstra, and then think of
optimizations/requirements.
Thanks again for the note!
Thanks for really good concise solutions again this year. Maybe @norvig and Karpathy are the two people who know the expressive power of python the best. I come back to these solutions every year to not only check alternate approaches but to also learn writing better Python.
https://github.com/norvig/pytudes/blob/main/ipynb/Advent-2024.ipynb
Since the distances are same throughout (1 unit) in Race Condition problem (Day 20), Dijkstra is overkill here although it obviously works perfectly fine. So BFS can asymptotically improve the running time.
The text was updated successfully, but these errors were encountered: