while (there are more arrivals or still cars in a Lane queue)
{
for (; time < whenThisStageEnds; time++)
{
while (there are more arrivals in this second)
{
// Add the next arrival to the appropriate queue
}
// Remove up to 1 car from the head of each queue that has a green
// light in this stage
}
// Move to the next stage
}