diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/elevator.py b/elevator.py index b44c167..ab0ba69 100644 --- a/elevator.py +++ b/elevator.py @@ -1,6 +1,7 @@ UP = 1 DOWN = 2 FLOOR_COUNT = 6 +OUT = 3 class ElevatorLogic(object): """ @@ -17,9 +18,13 @@ class ElevatorLogic(object): """ def __init__(self): - # Feel free to add any instance variables you want. self.destination_floor = None self.callbacks = None + self.destinations = [] + for i in range(FLOOR_COUNT + 1): + floors = {UP: 0, DOWN: 0, OUT: 0} + self.destinations.append(floors) + self.direction = None def on_called(self, floor, direction): """ @@ -30,7 +35,8 @@ def on_called(self, floor, direction): floor: the floor that the elevator is being called to direction: the direction the caller wants to go, up or down """ - self.destination_floor = floor + self.destinations[floor][direction] = 1 + self.on_ready() def on_floor_selected(self, floor): """ @@ -40,15 +46,27 @@ def on_floor_selected(self, floor): floor: the floor that was requested """ - self.destination_floor = floor + current_floor = self.callbacks.current_floor + if (self.direction == UP and floor <= current_floor or + self.direction == DOWN and floor >= current_floor): + return + self.destinations[floor][OUT] = 1 + self.on_ready() def on_floor_changed(self): """ This lets you know that the elevator has moved one floor up or down. You should decide whether or not you want to stop the elevator. """ - if self.destination_floor == self.callbacks.current_floor: - self.callbacks.motor_direction = None + floor = self.callbacks.current_floor + if floor == self.destination_floor: + if self.destinations[floor][self.direction]: + self.destinations[floor][self.direction] = 0 + elif self.destinations[floor][3 - self.direction]: + self.direction = 3 - self.direction + self.destinations[floor][self.direction] = 0 + self.destinations[floor][OUT] = 0 + self.callbacks.motor_direction = None def on_ready(self): """ @@ -56,7 +74,40 @@ def on_ready(self): Maybe passengers have embarked and disembarked. The doors are closed, time to actually move, if necessary. """ - if self.destination_floor > self.callbacks.current_floor: - self.callbacks.motor_direction = UP - elif self.destination_floor < self.callbacks.current_floor: - self.callbacks.motor_direction = DOWN + current_floor = self.callbacks.current_floor + direction, destination_floor, motor_direction = on_ready_impl( + current_floor, self.direction, self.destinations) + self.callbacks.motor_direction = motor_direction + self.direction = direction + if motor_direction == None and self.direction != None: + self.destinations[current_floor][OUT] = 0 + self.destinations[current_floor][self.direction] = 0 + self.destination_floor = destination_floor + + + +def on_ready_impl(floor, direction, destinations): + if direction == UP or direction is None: + for i in range(floor + 1, FLOOR_COUNT + 1): + if destinations[i][UP] or destinations[i][OUT]: + return UP, i, UP + + for i in range(FLOOR_COUNT, floor, -1): + if destinations[i][DOWN]: + return UP, i, UP + + if destinations[floor][DOWN]: + return DOWN, floor, None + + for i in range(floor - 1, 0, -1): + if destinations[i][DOWN] or destinations[i][OUT]: + return DOWN, i, DOWN + + for i in range(floor - 1, 0, -1): + if destinations[i][UP]: + return DOWN, i, DOWN + + if destinations[floor][UP]: + return UP, floor, None + + return None, None, None