Navigate the city
The government of your country has just completed a city designed to accommodate a few hundred thousand people. It has all sorts of facilities including roads, restaurants, malls, houses, etc. You have been invited to test the road system, but you are completely unfamiliar with it, so you need a GPS. Your challenge is to make one.
Description of the city
Note: when I say "a x a grid", I mean that there are a horizontal roads and a vertical roads within the grid.
To make the challenge easier (because of course making a GPS is difficult in itself), the city is fixed. The city's road system is based on a very rigid grid. This grid is pictured further below.
The grid has a size of 15x15 and the roads are labeled as
[r/c num][dir] where "r/c num" is the row/column number (whether it is a row or column depends on the direction of the road) and "dir" is the direction. If the road runs from west to east, "dir" is E, and if it runs from north to south, "dir" is S. For instance, "1E" is the first row of the grid, while "12S" is the twelfth column of the grid. Each row and column of the grid has a length of 14km (1km between each intersection).
The Central Business District (CBD) is the 5x5 sub-grid situated in the middle of the grid. Due to the traffic parameters set below, it is important for your GPS to route the user away from the CBD if it is not necessary to pass through that district. Note that the CBD includes the borders of the 5x5 sub-grid.
At each intersection, there is a traffic light. The amount of time to wait is as follows:
- there is a "base time" which is 400 if the intersection is in the CBD, and is the row position of the intersection (row 1 column 2 would be the intersection between 1E and 2S) times the column position otherwise;
- the waiting time to turn left is equal to the base time;
- the waiting time to go straight is equal to the base time, multiplied by 2;
- the waiting time to turn right is equal to the base time, multiplied by 3.
The times above are measured in seconds.
In addition to the standard grid of roads, there is an expressway circling the inner part of the city. It passes directly over parts of the 4E, 4S, 12E and 12S roads in such a way that it forms a square. In both directions, there is an innovative exit/entry elevator at each place where there would be an intersection if you took the 4E, 4S, 12E or 12S road rather than the expressway. It is positioned in such a way that you can skip the traffic light for that intersection and get going, and in addition to that you can turn your car around in whichever direction you would like and go that way. I know it sounds rather confusing, but in essence you can exit at any intersection and go whichever way you wish, with the distances still being measured in kilometers in all cases. Note that the elevator is instantaneous (once again, not very realistic, but who uses elevators for cars on the main road anyway?). Upon entering the expressway, you can go in either direction. On the expressway, there are no at-grade intersections, so the traffic is free-flowing, and there are no waits at traffic lights or any other obstruction. Entry onto the expressway is done at any intersection involving the 4E, 4S, 12E or 12S road. Cars entering the expressway do not need to wait at a traffic light before doing so. Once again, the way in which the entry elevator is designed ensures that all distances are still measured in kilometers. This means that you can drive 1km from an intersection, enter the expressway there, and drive 3km on the expressway, with the total distance being 4km.
The city sets speed limits as follows:
- 30 for all roads within the CBD;
- 60 for all standard roads outside the CBD;
- 120 for the expressway.
These speeds are measured in kilometers per hour.
Picture of the grid
Everything inside, and including, the bold black outline, is the Central Business District. The blue outline represents the expressway.
Write a GPS which takes in a start intersection and end intersection, and attempts to output the fastest route from the start to the end. The "intersection" must be an intersection between two roads and can be specified in whichever reasonable input format you would like. You could take the row and column, the names of the two roads, etc. as long as it's reasonable.
Your output should be a list of directions in whichever reasonable output format you would like. Each direction should specify the direction to turn (straight, left, right, and for the expressway, enter/exit), the road to turn on and the number of "kilometers after the previous direction" to wait before executing this direction. For instance, "After 7km, exit the expressway onto 12E (East)". If the direction is to be executed while on the expressway, it must not take intersections into account. Your program may optionally decide to omit directions telling the user to "continue straight", but it may not omit any other direction. As shown above, directions telling the user to enter or exit an expressway must specify the direction to head upon entry ("enter the expressway (South)", for instance). You may direct the user in any direction from the starting intersection, and arrive at the end intersection from any side. It should also tell the user for how long he or she will have to drive, down to the second, and, optionally, the distance he or she will have to drive, which is necessarily measured in kilometers.
Your GPS may assume that the user will always be driving at the speed limit. It may also assume that the start and end are different.
There are 225 x 224 = 50400 ways to choose start/end pairs. Your GPS will be scored based on the sum of the amounts of time it takes to drive from the start to the end, for each pair of start/end points. The GPS with the smallest total is the winner.
In the unlikely event of a tie between two or more submissions, length of code shall be the tiebreaker.
No test cases because there is never a single route for any start/end pair.
The tags for this challenge are code-challenge, path-finding, and grid.