Can You Escape The Traffic Jam (Again)?

Riddler Classic

Riddler Classic

A long time ago, there once was a riddle about cars getting caught in traffic jams. You could say that one really stuck with me.

In the original puzzle, there were N cars on a long, straight highway with a single lane. Each car traveled in the same direction but at a constant speed that was unique and randomly selected. However, if a driver caught up to another, slower car (or a group of cars similarly blocked by that slower car), they remained stuck behind that slower car.

This time around (as suggested by one of the original puzzle’s extension problems), a second lane opens up. The catch is that there is only one entry point into this second lane, so each car has exactly one opportunity to decide whether to proceed into this second lane.

From the first car in the original lane to the last car, each decides whether to make the switch to the second lane as it passes. Fortunately, each car knows the speed of every other car, so each will make the switch provided that it can ultimately proceed at a greater speed (whether on its own or as part of a faster group).

On average, how many total groups of cars will eventually form in the two lanes?

Solution

Let $N$ be the number of cars in the procession. There are $N!$ ways to order the cars by speed.

I ran code to determine the number of groups in the two lanes:

  1. The first car always stays in the first lane.
  2. The next car compares its speed to the last car in the first lane.
    • If the next car's speed is less than the last car in the first lane, it will stay in the first lane.
    • If the next car's speed is greater than the last car in the first lane, check the speed of the last car in the second lane.
      • If there are no cars in the second lane, move to the second lane.
      • If there are cars in the second lane, compare the speeds of the last car in the second lane to the last car of the first lane.
        • If the first lane is greater, stay in the first lane, and slow down to the speed of the last car in the first lane.
        • Move to the second lane, and compare speed to the speed of the last car in the second lane.
          • If the speed is greater, slow down to the speed of the last car in the second lane.
          • If the speed is less, drive at that speed.

  3. Repeat 2. for all additional cars.

Here is a plot for 2 - 10 cars.

Answer

I noticed the sum of all $N!$ groups is exactly this sequence.

The average number of groups is thus:

$$\sum\limits_{i=1}^N \left(\dfrac{1}{i} \cdot \sum\limits_{j=0}^{i-1} \dfrac{1}{j!} \right)$$

Rohan Lewis

2022.08.29

Code can be found here.