Fast code. Twice the belt time.
transcript
This code runs in microseconds. It still makes the trucks wait. Let us find out why.
Parcels on a belt have a priority class. Zero is express, one is standard, two is economy. Group them: express first, economy last.
The first attempt counts each class. Then it writes the classes back in order. For the test belt, that is 16 steps. Three counters of memory. Correct, and order n.
A central hub belt holds 3,000 parcels. Two passes make 6,000 steps. That is about 60 microseconds of computing. So computing is not the problem here.
But each pass is one full turn of the belt. One turn takes 6 minutes. Two passes take 12. The trucks load after one turn. So the first attempt takes twice the budget.
Here is the clue. Express always goes at the front. Economy always goes at the back. So why count first? Grow both blocks while you read.
Full breakdown in the lecture.