Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <stdlib.h>
- //r means roof floor
- int oneCurFloor = 1;
- int twoCurFloor = 1;
- std::string oneDir;
- std::string twoDir;
- int calls = 0;
- int oneCarrying = 0;
- int twoCarrying = 0;
- std::string callList [20];
- std::string newCallInput;
- int oneDestList[10];
- int twoDestList[10];
- void printElevator(int oneFloor, bool oneMoving, bool oneGoingUp, int twoFloor, bool twoMoving, bool twoGoingUp)
- {
- std::string firstRow = " ";
- if(oneMoving && oneGoingUp)
- {
- firstRow += "^ ";
- }
- else
- {
- firstRow += " ";
- }
- firstRow += "1 2 3 4 5 ";
- if(twoMoving && twoGoingUp)
- {
- firstRow += "^ ";
- }
- else
- {
- firstRow += " ";
- }
- firstRow += "1 2 3 4 5";
- std::cout << firstRow << std::endl;
- std::string secondRow = " ";
- for(int i = 0; i < 5; i++)
- {
- if(oneFloor == i + 1)
- {
- secondRow += "^ ";
- }
- else if(oneFloor == i + 6)
- {
- secondRow += "v ";
- }
- else
- {
- secondRow += " ";
- }
- }
- secondRow += " ";
- for(int i = 0; i < 5; i++)
- {
- if(twoFloor == i + 1)
- {
- secondRow += "^ ";
- }
- else if(twoFloor == i + 6)
- {
- secondRow += "v ";
- }
- else
- {
- secondRow += " ";
- }
- }
- std::cout << secondRow << std::endl;
- std::string thirdRow = " ";
- if(oneMoving && !oneGoingUp)
- {
- thirdRow += "v ";
- }
- else
- {
- thirdRow += " ";
- }
- thirdRow += "6 7 8 9 R ";
- if(twoMoving && !twoGoingUp)
- {
- thirdRow += "v";
- }
- else
- {
- thirdRow += " ";
- }
- thirdRow += "6 7 8 9 R";
- std::cout << thirdRow << std::endl;
- std::cout << " .---------------. .---------------." << std::endl;
- for(int j = 0; j < 15; j++)
- {
- std::cout << " || | || || | ||" << std::endl;
- }
- std::cout << " '---------------' '---------------'" << std::endl;
- }
- std::string inputToArr(int callFloor, bool dirUp)
- {
- std::string out;
- if(callFloor == 10)
- {
- out += "R";
- }
- else
- {
- out += std::to_string(callFloor);
- }
- out += "";
- if(dirUp)
- {
- out += "U";
- }
- else
- {
- out += "D";
- }
- return out;
- }
- std::string lowerCaser(std::string input)
- {
- std::string output;
- char c;
- for(unsigned i = 0; i < input.length(); i++)
- {
- c = input[i];
- output += (char) tolower(c);
- }
- return output;
- }
- int newInput()
- {
- std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
- std::cin >> newCallInput;
- newCallInput = lowerCaser(newCallInput);
- while(newCallInput != "n" && newCallInput != "y")
- {
- std::cout << "SPEAK CLEARLY." << std::endl;
- std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
- std::cin >> newCallInput;
- newCallInput = lowerCaser(newCallInput);
- }
- if(newCallInput == "n" && calls == 0 && oneCarrying == 0 && oneCurFloor == 1 && twoCarrying == 0 && twoCurFloor == 1)
- {
- std::cout << "Alright. Guess we're done here then." << std::endl;
- return 0;
- }
- else if(newCallInput == "y")
- {
- std::string callFloorIn;
- std::string dirIn;
- int floor;
- bool dirUp;
- std::cout << "Input the floor to call from, from 1-10." << std::endl;
- std::cin >> callFloorIn;
- while (callFloorIn != "1" && callFloorIn != "2" && callFloorIn != "3" && callFloorIn != "4" &&
- callFloorIn != "5" && callFloorIn != "6" && callFloorIn != "7" && callFloorIn != "8" &&
- callFloorIn != "9" && callFloorIn != "10") {
- std::cout << "SPEAK CLEARLY." << std::endl;
- std::cout << "Input the floor to call from, from 1-10." << std::endl;
- std::cin >> callFloorIn;
- }
- floor = std::stoi(callFloorIn);
- std::cout << "Are you going up (U) or down (D)?" << std::endl;
- std::cin >> dirIn;
- dirIn = lowerCaser(dirIn);
- while(dirIn != "u" && dirIn != "d")
- {
- std::cout << "You stubbed your finger on the input panel. Try again." << std::endl;
- std::cout << "Are you going up (U) or down (D)?" << std::endl;
- std::cin >> dirIn;
- dirIn = lowerCaser(dirIn);
- }
- dirUp = dirIn == "u";
- callList[calls] = inputToArr(floor, dirUp);
- calls++;
- return 1;
- }
- else
- {
- return 1;
- }
- }
- void pickUp(int num)
- {
- bool realPickUp = false;
- std::string firstCall = callList[0];
- if(num == 1)
- {
- if(calls == 1 && firstCall[1] == oneDir[0] && ((firstCall[0] == 'R' && oneCurFloor == 10) || (firstCall[0] - 48 == oneCurFloor)))
- {
- realPickUp = true;
- callList[0] = "";
- }
- else
- {
- for(int i = 0; i < 20; i++)
- {
- std::string cur = callList[i];
- if((cur[0] == 'R' && oneCurFloor == 10 || cur[0] - 48 == oneCurFloor) && (oneCarrying == 0 || cur[1] == oneDir[0]))
- {
- realPickUp = true;
- callList[i] = "";
- }
- }
- }
- }
- else
- {
- if(calls == 1 && firstCall[1] == twoDir[0] && ((firstCall[0] == 'R' && twoCurFloor == 10) || (firstCall[0] - 48 == twoCurFloor)))
- {
- realPickUp = true;
- callList[0] = "";
- }
- else
- {
- for(int i = 0; i < 20; i++)
- {
- std::string cur = callList[i];
- if((cur[0] == 'R' && twoCurFloor == 10 || cur[0] - 48 == twoCurFloor) && (twoCarrying == 0 || cur[1] == twoDir[0]))
- {
- realPickUp = true;
- callList[i] = "";
- }
- }
- }
- }
- if(realPickUp)
- {
- std::cout << "Elevator " + std::to_string(num) + " picking up passengers." << std::endl;
- std::string passInput;
- int passengers;
- std::cout << "How many people are getting on at this floor?" << std::endl;
- std::cin >> passInput;
- while(passInput != "1" && passInput != "2" && passInput != "3" && passInput != "4" && passInput != "5" && passInput != "6" && passInput != "7" && passInput != "8" && passInput != "9" && passInput != "10")
- {
- std::cout << "You appear to have found Schrodinger's elevator passenger. Please try again." << std::endl;
- std::cout << "How many people are getting on at this floor?" << std::endl;
- std::cin >> passInput;
- }
- passengers = std::stoi(passInput);
- if((num == 1 && oneCarrying + passengers > 10) || (num == 2 && twoCarrying + passengers > 10))
- {
- passengers = 10 - oneCarrying;
- if(num == 2)
- {
- passengers = 10 - twoCarrying;
- }
- std::cout << "Unfortunately the elevator currently has too many passengers. Only the first " + std::to_string(passengers) + " passengers can get on." << std::endl;
- }
- for(int i = 0; i < passengers; i++)
- {
- std::string destFloorIn;
- int destFloor;
- std::string output = "What floor is the " + std::to_string(i + 1);
- if(i + 1 == 1)
- {
- output += "st";
- }
- else if(i + 1 == 2)
- {
- output += "nd";
- }
- else if(i + 1 == 3)
- {
- output += "rd";
- }
- else
- {
- output += "th";
- }
- output += " passenger going to? (Numbers 1-9, or R for the roof floor)";
- std::cout << output << std::endl;
- std::cin >> destFloorIn;
- while(destFloorIn != "R" && destFloorIn != "r" && destFloorIn != "1" && destFloorIn != "2" && destFloorIn != "3" && destFloorIn != "4" && destFloorIn != "5" && destFloorIn != "6" && destFloorIn != "7" && destFloorIn != "8" && destFloorIn != "9")
- {
- std::cout << "I'm sorry, but you haven't exactly floored me with your response. Please try again." << std::endl;
- std::cout << output << std::endl;
- std::cin >> destFloorIn;
- }
- if(destFloorIn == "R" || destFloorIn == "r")
- {
- destFloor = 10;
- }
- else
- {
- destFloor = std::stoi(destFloorIn);
- }
- if(num == 1)
- {
- oneDestList[oneCarrying + 1] = destFloor;
- oneCarrying++;
- }
- else
- {
- twoDestList[twoCarrying + 1] = destFloor;
- twoCarrying++;
- }
- }
- calls--;
- }
- }
- void dropOff()
- {
- for(int i = 0; i < 10; i++)
- {
- if(oneDestList[i] == oneCurFloor)
- {
- oneDestList[i] = 0;
- oneCarrying--;
- }
- if(twoDestList[i] == twoCurFloor)
- {
- twoDestList[i] = 0;
- twoCarrying--;
- }
- }
- for(int i = 0; i < 10; i++)
- {
- if(oneDestList[i] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(oneDestList[j] != 0)
- {
- oneDestList[i] = oneDestList[j];
- oneDestList[j] = 0;
- }
- }
- }
- if(twoDestList[i] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(twoDestList[j] != 0)
- {
- twoDestList[i] = twoDestList[j];
- twoDestList[j] = 0;
- }
- }
- }
- }
- }
- void bringDown()
- {
- for(int i = 0; i < 20; i++)
- {
- if(callList[i].empty())
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(!callList[j].empty())
- {
- callList[i] = callList[j];
- callList[j] = "";
- }
- }
- }
- if(oneDestList[i % 10] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(oneDestList[j] != 0)
- {
- oneDestList[i] = oneDestList[j];
- oneDestList[j] = 0;
- }
- }
- }
- if(twoDestList[i % 10] == 0)
- {
- for(int j = i + 1; j < 10; j++)
- {
- if(twoDestList[j] != 0)
- {
- twoDestList[i] = twoDestList[j];
- twoDestList[j] = 0;
- }
- }
- }
- }
- }
- void moveFloor(bool goingUp, int num)
- {
- if(num == 1)
- {
- if(goingUp)
- {
- oneDir = "U";
- oneCurFloor++;
- if(oneCurFloor == 10)
- {
- oneDir = "N";
- }
- }
- else
- {
- oneDir = "D";
- oneCurFloor--;
- if(oneCurFloor == 1)
- {
- oneDir = "N";
- }
- }
- dropOff();
- pickUp(1);
- }
- if(num == 2)
- {
- if(goingUp)
- {
- twoDir = "U";
- twoCurFloor++;
- if(twoCurFloor == 10)
- {
- twoDir = "N";
- }
- }
- else
- {
- twoDir = "D";
- twoCurFloor--;
- if(twoCurFloor == 1)
- {
- twoDir = "N";
- }
- }
- dropOff();
- pickUp(2);
- }
- dropOff(); //covers a passenger picking the same floor as they're on
- }
- void carryPassengers(int num)
- {
- int destFloor = 100;
- for(int i = 0; i < 10; i++)
- {
- int cur;
- if(num == 1)
- {
- cur = oneDestList[i];
- if(abs(oneCurFloor - cur) < abs(oneCurFloor - destFloor))
- {
- destFloor = cur;
- }
- }
- else
- {
- cur = twoDestList[i];
- if(abs(twoCurFloor - cur) < abs(twoCurFloor - destFloor))
- {
- destFloor = cur;
- }
- }
- }
- moveFloor((num == 1 && oneCurFloor < destFloor) || (num == 2 && twoCurFloor < destFloor), num);
- }
- int main()
- {
- printElevator(1, false, false, 1, false, false);
- oneDir = "N";
- twoDir = "N";
- while(true) {
- int rtn = newInput();
- if (rtn == 0) {
- std::cout << "Have a nice day!" << std::endl;
- return 0;
- }
- bringDown();
- std::cout << "Calls: " + std::to_string(calls) << std::endl;
- std::cout << "Elevator One: Carrying " + std::to_string(oneCarrying) << std::endl;
- std::cout << "Elevator Two: Carrying " + std::to_string(twoCarrying) << std::endl;
- //elevator procedure
- if(calls == 0)
- {
- if(oneCurFloor > 1 && oneCarrying == 0)
- {
- moveFloor(false, 1);
- }
- else if(oneCarrying > 0)
- {
- carryPassengers(1);
- }
- if(twoCurFloor > 1 && twoCarrying == 0)
- {
- moveFloor(false, 2);
- }
- else if(twoCarrying > 0)
- {
- carryPassengers(2);
- }
- }
- else if(oneCarrying == 0)
- {
- std::cout << "Send One to the nearest call." << std::endl;
- if(twoCarrying == 0)
- {
- //send One to the nearest call
- int oneTargetFloor = 0;
- int oneLowestDist = 100;
- int oneTargetIndex = -1;
- int twoTargetFloor = 0;
- int twoLowestDist = 100;
- for(int i = 0; i < 20; i++)
- {
- std::string cur = callList[i];
- int callFloor;
- if(cur[0] == 'R')
- {
- callFloor = 10;
- }
- else
- {
- callFloor = cur[0] - 48;
- }
- if(abs(oneCurFloor - callFloor) < oneLowestDist)
- {
- oneTargetFloor = callFloor;
- oneLowestDist = abs(oneCurFloor - callFloor);
- oneTargetIndex = i;
- }
- }
- if(calls == 1)
- {
- if(twoCurFloor > 1)
- {
- moveFloor(false, 2);
- }
- }
- else
- {
- //UGH... Send Two to the other call.
- for(int j = 0; j < 20; j++)
- {
- if(j != oneTargetIndex)
- {
- std::string cur = callList[j];
- int callFloor;
- if(cur[0] == 'R')
- {
- callFloor = 10;
- }
- else
- {
- callFloor = cur[0] - 48;
- }
- if(abs(twoCurFloor - callFloor) < twoLowestDist)
- {
- twoTargetFloor = callFloor;
- twoLowestDist = abs(twoCurFloor - callFloor);
- }
- }
- }
- }
- if(oneTargetFloor > oneCurFloor)
- {
- moveFloor(true, 1);
- }
- else if(oneTargetFloor < oneCurFloor)
- {
- moveFloor(false, 1);
- }
- if(twoTargetFloor > twoCurFloor && twoCurFloor != 10)
- {
- moveFloor(true, 2);
- }
- else if(twoTargetFloor < twoCurFloor && twoCurFloor != 1)
- {
- moveFloor(false, 2);
- }
- }
- else
- {
- //send One (empty) to the nearest call not in Two's direction, if there is one
- int oneTarget;
- int targetDist = 100;
- for(int i = 0; i < 20; i++)
- {
- std::string curCall = callList[i];
- int curCallFloor;
- if(!curCall.empty())
- {
- if(curCall[0] == 'R')
- {
- curCallFloor = 10;
- }
- else
- {
- curCallFloor = curCall[0] - 48;
- }
- if(curCall[1] != twoDir[0] && curCallFloor - oneCurFloor < targetDist)
- {
- oneTarget = curCallFloor;
- targetDist = curCallFloor - oneCurFloor;
- }
- }
- }
- if(targetDist == 100)
- {
- //send One to ground floor if it's not already there - Two can pick up the calls
- if(oneCurFloor > 1)
- {
- moveFloor(false, 1);
- }
- }
- else
- {
- moveFloor(oneTarget > oneCurFloor, 1);
- }
- carryPassengers(2);
- }
- }
- else if(twoCarrying == 0)
- {
- //send Two (empty) to the nearest call not in One's direction, if there is one
- int twoTarget;
- int targetDist = 100;
- for(int i = 0; i < 20; i++)
- {
- std::string curCall = callList[i];
- int curCallFloor;
- if(!curCall.empty())
- {
- if(curCall[0] == 'R')
- {
- curCallFloor = 10;
- }
- else
- {
- curCallFloor = curCall[0] - 48;
- }
- if(curCall[1] != oneDir[0] && curCallFloor - twoCurFloor < targetDist)
- {
- twoTarget = curCallFloor;
- targetDist = curCallFloor - twoCurFloor;
- }
- }
- }
- if(targetDist == 100)
- {
- //send Two to ground floor if it's not already there - One can pick up the calls
- if(twoCurFloor > 1)
- {
- moveFloor(false, 2);
- }
- }
- else
- {
- moveFloor(twoTarget > twoCurFloor, 1);
- }
- carryPassengers(1);
- }
- else
- {
- //pick up passengers if they're on the way, otherwise just keep going
- carryPassengers(1);
- carryPassengers(2);
- }
- printElevator(oneCurFloor, oneDir != "N", oneDir == "U", twoCurFloor, twoDir != "N", twoDir == "U");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment