Skip to content

Instantly share code, notes, and snippets.

@nitschmann
Last active August 7, 2024 17:17
Show Gist options
  • Save nitschmann/c3b0987a7c20c19f02e3791888deb923 to your computer and use it in GitHub Desktop.
Save nitschmann/c3b0987a7c20c19f02e3791888deb923 to your computer and use it in GitHub Desktop.
Elevator Simulator

Challenge overview

The goal is the implementation a basic elevator simulation. The elevator stops at each requested floor. There are not such things as parallel calls for this challange, everything is rather sequential for this challenge.

Conditions and requirements

  1. The simulator runs everything within the main() function for now
  2. the elevator range is between a base floor (0) and maximum floor (6), these are statically defined as constants in the script
  3. The elevator should accept floor requests (which can simulate persons standing in a specific floor and requesting the lift) and process the requested floors in the order they have been received
  4. When the elevator moves it should log the floors it passes and it should log its arrival in the requested floor
  5. consecutive duplicate requests (e.g. 4,4, 1, 2, 2) should be removed to prevent the elevator from stopping at the same floor multiple times in a row
  6. In the simulation it starts in a idle state and initial requests should trigger the movement

How to run

  1. Start is at basement floor as default
  2. Multiple requests are being made
  3. Requests are being processed and the elevator should move inbetween those requests, until no request is left

Potential issues + solutions

  • Simulation variance and input: the simulation for now is completely static and the order fixed. For more real-world (test) scenarios the input could be via CLI command and the requests could be simulated in a random list
  • Idle state: Atm there is no automated way to move the elvator back from idle state into movement
  • Request Order: Requests are currently processed in the order they were received. This is usually not the most efficient route, a different scheduling and grouping of requests could be maybe helpful here
  • Scalability: The number of requests could grow and the scripts become more inefficient, due to repeating list operations
  • No real time and async processing: Everything is for now sequentially processed, this is not close to reality
package main
import "fmt"
const (
basementFloor = 0
maxFloor = 6
)
var (
currentFloor int
requests []int
)
func logPassingFloors(currentFloor, destination int) {
if currentFloor < destination {
for i := currentFloor + 1; i < destination; i++ {
fmt.Printf("Passing floor %d...\n", i)
}
} else if currentFloor > destination {
for i := currentFloor - 1; i > destination; i-- {
fmt.Printf("Passing floor %d...\n", i)
}
}
}
func move() {
if len(requests) == 0 {
fmt.Println("Elevator is idle\n")
return
}
destination := requests[0]
if destination > currentFloor {
fmt.Printf("Moving up to floor %d\n", destination)
logPassingFloors(currentFloor, destination)
} else if destination < currentFloor {
fmt.Printf("Moving down to floor %d\n", destination)
logPassingFloors(currentFloor, destination)
} else {
fmt.Printf("Elavator has arrived at destination floor %d\n", destination)
requests = append(requests[:0], requests[1:]...)
}
currentFloor = destination
}
func requestElevator(floor int) {
if floor < basementFloor || floor > maxFloor {
fmt.Printf("Invalid floor %d\n", floor)
return
}
requests = append(requests, floor)
// remove consecutive duplicates
if len(requests) > 1 {
cleanedRequests := []int{(requests[0])}
for i := 1; i < len(requests); i++ {
if requests[i] != requests[i-1] {
cleanedRequests = append(cleanedRequests, requests[i])
}
}
requests = cleanedRequests
}
}
func main() {
move()
requestElevator(5)
requestElevator(4)
requestElevator(2)
requestElevator(0)
requestElevator(2)
requestElevator(2)
requestElevator(6)
requestElevator(6)
requestElevator(0)
for {
move()
if len(requests) == 0 {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment