Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nice problem :) #13

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
69 changes: 60 additions & 9 deletions elevator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
UP = 1
DOWN = 2
FLOOR_COUNT = 6
OUT = 3

class ElevatorLogic(object):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -40,23 +46,68 @@ 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):
"""
This is called when the elevator is ready to go.
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