You are given a building with M floors (0-indexed 0 … M-1) and N elevators. Each elevator is described by its current floor, its current direction (UP, DOWN, or IDLE), and its unique index. When a passenger on some floor F presses the UP or DOWN button, the system must immediately choose exactly one elevator to serve that call. Write a function that, for each incoming hall call, returns the index of the elevator that should be dispatched.
Dispatch rules — group elevators into three priority buckets in the order below:
Tie-breaker inside every bucket: choose the elevator with the smallest absolute distance to floor F. If still tied, choose the elevator with the smallest index. If no elevators exist (extremely corner case) return -1.
Your function will be called separately for each hall call; you do not simulate movement or update state—just implement the selection logic described.