The purpose of a roundabout is to control the flow of traffic at a busy intersection. A roundabout has 4 entry points: from the North, East, South and West; and 4 exit points at the same locations (see Example 0 for a diagram).
Cars can enter, leave or move one position on the roundabout. Each of those events requires one second. All the events can take place simultaneously. So if there are two cars on the roundabout, one behind the other, then they can both move during the next second. A car decides whether to enter the roundabout based on its knowledge from the previous second. A car will always enter if it has the right to do so.
A car has the right to enter only if there are no cars to its immediate left (either on the roundabout or waiting to get on the roundabout) and it is the first car in line at the entry point (Example 1 demonstrates this). A special case occurs if there is a car at each of the 4 entry points. In this case, the car from the North will wait until there are no cars to its left on the roundabout and then will be the first to enter.
Once the car is on the roundabout, it moves counter-clockwise until it exits the roundabout. There are 4 positions on the roundabout. So for example, it would take 1 second to enter the roundabout, 2 seconds to complete half a lap and another 1 second to exit the roundabout.
Each parameter of the input is a second by second account of cars coming from that direction with their intended exit locations. Intended exit locations will be N (North), E (East), S (South) and W (West). A '-' means that no car arrived during that second at the given entry point. Thus, for example, character i of north represents the direction in which a car (if there is one) arriving from the north at time i will leave the roundabout. So, at time i, this car will be added to the end of the line at the north entry point to the roundabout. Cars will not be allowed to exit the roundabout at the point of their entry.
Given the lists of cars coming from the 4 directions, return the total time required for all cars to leave the roundabout.
|