Project 12 of 15, for Neil
Route Planner
A route planner for Neil's race weekend: Dijkstra round the circuit with minutes on every road, then A* across the car park, heading for his car, with mud at three times the cost.
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilThe fewest roads isn't always quickest, because going straight from the campsite to the main gate is eight minutes of queue.
- NeilRound by the food court is a road longer and a minute quicker, and my feet honestly don't count roads.
- PaulThen write the minutes on the roads, and always carry on from the nearest place you haven't finished with.
- NeilMud in the car park takes three times as long as the track, too, and it always rains on race day.
MINUTES in routes.py is the circuit again, with the minutes each road takes on foot: MINUTES["campsite"]["food-court"] is 4. Roads can still be one way, and a place with no dictionary of its own has no roads out. No road takes less than 0 minutes.
route_minutes(roads, route)gives back the minutes along a route, a list of places; a route of one place, or none, takes 0. If two places next to each other in the route have no road between them, it raisesValueError, naming both.quickest_times(roads, start)gives back a dictionary of every place that can be reached fromstart, and the fewest minutes it takes to get there.quickest_route(roads, start, goal)gives back(minutes, route)for the quickest route,(0, [start])if they're the same place, orNoneif there's no way there. If two routes tie, either will do.
Use heapq, and look at each place's roads once.
Then the car park, in planner.py. park.txt is its map: # is hedge, . track, ~ mud, S the gate and G Neil's car. read_park(text) and neighbours(tiles, tile) are the maze solver's, written for you, with a cost on every tile: 1 for track, S and G, 3 for mud, None for hedge.
manhattan(tile, goal)is the rows apart plus the columns apart.a_star(tiles, start, goal)gives back(minutes, route)for the quickest route across the car park, whereroutelists the tiles fromstarttogoalandminutesadds up the cost of every tile stepped onto after the start.(0, [start])if they're the same tile,Noneif there's no way there. Use A* withmanhattanas the estimate. If routes tie, any will do.draw_route(tiles, route)gives back the car park as a list of strings,#,.and~, with each tile of the route but the last showing which way to step out of it (^,v,<or>), andGon the last.
Fewest roads to the hairpin: 16 minutes Quickest to the hairpin: 14 minutes, by campsite, food-court, merch-stand, grandstand, paddock, hairpin Furthest from the campsite: viewing-bank, 24 minutes Gate to car: 23 minutes, 19 steps v.....#.......#..... v####.#.~~~~~.#.###. v#....#.~###~...#... v#.####.~#G.~####.#. v#......~#^#~.....#. v######.~#^#~####.#. >>>>>>>>>>^~~...#.#. .####.#######.#.#... ......#.......#...#.
What you learn
What's in it
- Minutes on the roads
- Minutes, not roads
- Nearest unfinished place first
- In order of minutes
- How long Paul's walks take
- Fix Caitlin's version
- Make Neil's quickest routes
- Dijkstra, with a sense of direction
- Which tile next
- Make the car park planner
- Ship it