Paul's Python CoursePPC

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.

About 4 hours, in 12 steps.

Sign up free to open itLog in

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.

  1. 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 raises ValueError, naming both.
  2. quickest_times(roads, start) gives back a dictionary of every place that can be reached from start, and the fewest minutes it takes to get there.
  3. quickest_route(roads, start, goal) gives back (minutes, route) for the quickest route, (0, [start]) if they're the same place, or None if 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.

  1. manhattan(tile, goal) is the rows apart plus the columns apart.
  2. a_star(tiles, start, goal) gives back (minutes, route) for the quickest route across the car park, where route lists the tiles from start to goal and minutes adds up the cost of every tile stepped onto after the start. (0, [start]) if they're the same tile, None if there's no way there. Use A* with manhattan as the estimate. If routes tie, any will do.
  3. 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 >), and G on the last.
What it prints
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

  1. Minutes on the roads
  2. Minutes, not roads
  3. Nearest unfinished place first
  4. In order of minutes
  5. How long Paul's walks take
  6. Fix Caitlin's version
  7. Make Neil's quickest routes
  8. Dijkstra, with a sense of direction
  9. Which tile next
  10. Make the car park planner
  11. Ship it