Buttons 9, 1, 8: why your elevator shouldn't obey the order
transcript
Floor 5, going up. Someone presses 9. Then 1. Then 8. Serve them in that order? Someone rides past their floor. Twice.
Up to 9. Four floors. The 8 rider rides straight past 8. All the way down to 1. Eight more. Then back up to 8. Seven more. They passed it twice.
Press order: 19 floors. Now serve them as 8, 9, then 1. Just 12 floors.
A list of floors to visit gives you the zig-zag. Real controllers use LOOK. Keep going while there are stops ahead. Turn round at the last requested stop. Not the top floor. And no rider is carried past their floor and back.
In code, it's a few states. Idle gets a stop above? Move up at once. A stop on this floor? Open the doors. Doors close, stops still ahead? Keep going. Only stops behind? Only then turn round.
Don't obey the press order. Sweep, then turn.
Full breakdown in the lecture.