nitrodog96

Double Elevator

Oct 26th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdlib.h>
  5.  
  6. //r means roof floor
  7. int oneCurFloor = 1;
  8. int twoCurFloor = 1;
  9. std::string oneDir;
  10. std::string twoDir;
  11. int calls = 0;
  12. int oneCarrying = 0;
  13. int twoCarrying = 0;
  14. std::string callList [20];
  15. std::string newCallInput;
  16. int oneDestList[10];
  17. int twoDestList[10];
  18.  
  19. void printElevator(int oneFloor, bool oneMoving, bool oneGoingUp, int twoFloor, bool twoMoving, bool twoGoingUp)
  20. {
  21.     std::string firstRow = "      ";
  22.     if(oneMoving && oneGoingUp)
  23.     {
  24.         firstRow += "^ ";
  25.     }
  26.     else
  27.     {
  28.         firstRow += "  ";
  29.     }
  30.     firstRow += "1 2 3 4 5          ";
  31.     if(twoMoving && twoGoingUp)
  32.     {
  33.         firstRow += "^ ";
  34.     }
  35.     else
  36.     {
  37.         firstRow += "  ";
  38.     }
  39.     firstRow += "1 2 3 4 5";
  40.     std::cout << firstRow << std::endl;
  41.  
  42.     std::string secondRow = "        ";
  43.     for(int i = 0; i < 5; i++)
  44.     {
  45.         if(oneFloor == i + 1)
  46.         {
  47.             secondRow += "^ ";
  48.         }
  49.         else if(oneFloor == i + 6)
  50.         {
  51.             secondRow += "v ";
  52.         }
  53.         else
  54.         {
  55.             secondRow += "  ";
  56.         }
  57.     }
  58.     secondRow += "           ";
  59.     for(int i = 0; i < 5; i++)
  60.     {
  61.         if(twoFloor == i + 1)
  62.         {
  63.             secondRow += "^ ";
  64.         }
  65.         else if(twoFloor == i + 6)
  66.         {
  67.             secondRow += "v ";
  68.         }
  69.         else
  70.         {
  71.             secondRow += "  ";
  72.         }
  73.     }
  74.     std::cout << secondRow << std::endl;
  75.  
  76.     std::string thirdRow = "      ";
  77.     if(oneMoving && !oneGoingUp)
  78.     {
  79.         thirdRow += "v ";
  80.     }
  81.     else
  82.     {
  83.         thirdRow += "  ";
  84.     }
  85.     thirdRow += "6 7 8 9 R          ";
  86.     if(twoMoving && !twoGoingUp)
  87.     {
  88.         thirdRow += "v";
  89.     }
  90.     else
  91.     {
  92.         thirdRow += "  ";
  93.     }
  94.     thirdRow += "6 7 8 9 R";
  95.     std::cout << thirdRow << std::endl;
  96.  
  97.  
  98.     std::cout << "    .---------------.    .---------------." << std::endl;
  99.     for(int j = 0; j < 15; j++)
  100.     {
  101.         std::cout << "    ||      |      ||    ||      |      ||" << std::endl;
  102.     }
  103.     std::cout << "    '---------------'    '---------------'" << std::endl;
  104.  
  105. }
  106.  
  107. std::string inputToArr(int callFloor, bool dirUp)
  108. {
  109.     std::string out;
  110.     if(callFloor == 10)
  111.     {
  112.         out += "R";
  113.     }
  114.     else
  115.     {
  116.         out += std::to_string(callFloor);
  117.     }
  118.  
  119.     out += "";
  120.  
  121.     if(dirUp)
  122.     {
  123.         out += "U";
  124.     }
  125.     else
  126.     {
  127.         out += "D";
  128.     }
  129.     return out;
  130. }
  131.  
  132. std::string lowerCaser(std::string input)
  133. {
  134.     std::string output;
  135.     char c;
  136.     for(unsigned i = 0; i < input.length(); i++)
  137.     {
  138.         c = input[i];
  139.         output += (char) tolower(c);
  140.     }
  141.     return output;
  142. }
  143.  
  144. int newInput()
  145. {
  146.     std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
  147.     std::cin >> newCallInput;
  148.     newCallInput = lowerCaser(newCallInput);
  149.     while(newCallInput != "n" && newCallInput != "y")
  150.     {
  151.         std::cout << "SPEAK CLEARLY." << std::endl;
  152.         std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
  153.         std::cin >> newCallInput;
  154.         newCallInput = lowerCaser(newCallInput);
  155.     }
  156.  
  157.     if(newCallInput == "n" && calls == 0 && oneCarrying == 0 && oneCurFloor == 1 && twoCarrying == 0 && twoCurFloor == 1)
  158.     {
  159.         std::cout << "Alright. Guess we're done here then." << std::endl;
  160.         return 0;
  161.     }
  162.     else if(newCallInput == "y")
  163.     {
  164.         std::string callFloorIn;
  165.         std::string dirIn;
  166.         int floor;
  167.         bool dirUp;
  168.  
  169.         std::cout << "Input the floor to call from, from 1-10." << std::endl;
  170.         std::cin >> callFloorIn;
  171.         while (callFloorIn != "1" && callFloorIn != "2" && callFloorIn != "3" && callFloorIn != "4" &&
  172.                callFloorIn != "5" && callFloorIn != "6" && callFloorIn != "7" && callFloorIn != "8" &&
  173.                callFloorIn != "9" && callFloorIn != "10") {
  174.             std::cout << "SPEAK CLEARLY." << std::endl;
  175.             std::cout << "Input the floor to call from, from 1-10." << std::endl;
  176.             std::cin >> callFloorIn;
  177.         }
  178.         floor = std::stoi(callFloorIn);
  179.  
  180.         std::cout << "Are you going up (U) or down (D)?" << std::endl;
  181.         std::cin >> dirIn;
  182.         dirIn = lowerCaser(dirIn);
  183.         while(dirIn != "u" && dirIn != "d")
  184.         {
  185.             std::cout << "You stubbed your finger on the input panel. Try again." << std::endl;
  186.             std::cout << "Are you going up (U) or down (D)?" << std::endl;
  187.             std::cin >> dirIn;
  188.             dirIn = lowerCaser(dirIn);
  189.         }
  190.  
  191.         dirUp = dirIn == "u";
  192.         callList[calls] = inputToArr(floor, dirUp);
  193.         calls++;
  194.  
  195.         return 1;
  196.     }
  197.     else
  198.     {
  199.         return 1;
  200.     }
  201. }
  202.  
  203. void pickUp(int num)
  204. {
  205.     bool realPickUp = false;
  206.     std::string firstCall = callList[0];
  207.  
  208.     if(num == 1)
  209.     {
  210.         if(calls == 1 && firstCall[1] == oneDir[0] && ((firstCall[0] == 'R' && oneCurFloor == 10) || (firstCall[0] - 48 == oneCurFloor)))
  211.         {
  212.             realPickUp = true;
  213.             callList[0] = "";
  214.         }
  215.         else
  216.         {
  217.             for(int i = 0; i < 20; i++)
  218.             {
  219.                 std::string cur = callList[i];
  220.                 if((cur[0] == 'R' && oneCurFloor == 10 || cur[0] - 48 == oneCurFloor) && (oneCarrying == 0 || cur[1] == oneDir[0]))
  221.                 {
  222.                     realPickUp = true;
  223.                     callList[i] = "";
  224.                 }
  225.             }
  226.         }
  227.     }
  228.     else
  229.     {
  230.         if(calls == 1 && firstCall[1] == twoDir[0] && ((firstCall[0] == 'R' && twoCurFloor == 10) || (firstCall[0] - 48 == twoCurFloor)))
  231.         {
  232.             realPickUp = true;
  233.             callList[0] = "";
  234.         }
  235.         else
  236.         {
  237.             for(int i = 0; i < 20; i++)
  238.             {
  239.                 std::string cur = callList[i];
  240.                 if((cur[0] == 'R' && twoCurFloor == 10 || cur[0] - 48 == twoCurFloor) && (twoCarrying == 0 || cur[1] == twoDir[0]))
  241.                 {
  242.                     realPickUp = true;
  243.                     callList[i] = "";
  244.                 }
  245.             }
  246.         }
  247.     }
  248.  
  249.     if(realPickUp)
  250.     {
  251.         std::cout << "Elevator " + std::to_string(num) + " picking up passengers." << std::endl;
  252.         std::string passInput;
  253.         int passengers;
  254.  
  255.         std::cout << "How many people are getting on at this floor?" << std::endl;
  256.         std::cin >> passInput;
  257.  
  258.         while(passInput != "1" && passInput != "2" && passInput != "3" && passInput != "4" && passInput != "5" && passInput != "6" && passInput != "7" && passInput != "8" && passInput != "9" && passInput != "10")
  259.         {
  260.             std::cout << "You appear to have found Schrodinger's elevator passenger. Please try again." << std::endl;
  261.             std::cout << "How many people are getting on at this floor?" << std::endl;
  262.             std::cin >> passInput;
  263.         }
  264.         passengers = std::stoi(passInput);
  265.  
  266.         if((num == 1 && oneCarrying + passengers > 10) || (num == 2 && twoCarrying + passengers > 10))
  267.         {
  268.             passengers = 10 - oneCarrying;
  269.             if(num == 2)
  270.             {
  271.                 passengers = 10 - twoCarrying;
  272.             }
  273.             std::cout << "Unfortunately the elevator currently has too many passengers. Only the first " + std::to_string(passengers) + " passengers can get on." << std::endl;
  274.         }
  275.         for(int i = 0; i < passengers; i++)
  276.         {
  277.             std::string destFloorIn;
  278.             int destFloor;
  279.             std::string output = "What floor is the " + std::to_string(i + 1);
  280.             if(i + 1 == 1)
  281.             {
  282.                 output += "st";
  283.             }
  284.             else if(i + 1 == 2)
  285.             {
  286.                 output += "nd";
  287.             }
  288.             else if(i + 1 == 3)
  289.             {
  290.                 output += "rd";
  291.             }
  292.             else
  293.             {
  294.                 output += "th";
  295.             }
  296.             output += " passenger going to? (Numbers 1-9, or R for the roof floor)";
  297.             std::cout << output << std::endl;
  298.             std::cin >> destFloorIn;
  299.  
  300.             while(destFloorIn != "R" && destFloorIn != "r" && destFloorIn != "1" && destFloorIn != "2" && destFloorIn != "3" && destFloorIn != "4" && destFloorIn != "5" && destFloorIn != "6" && destFloorIn != "7" && destFloorIn != "8" && destFloorIn != "9")
  301.             {
  302.                 std::cout << "I'm sorry, but you haven't exactly floored me with your response. Please try again." << std::endl;
  303.                 std::cout << output << std::endl;
  304.                 std::cin >> destFloorIn;
  305.             }
  306.  
  307.             if(destFloorIn == "R" || destFloorIn == "r")
  308.             {
  309.                 destFloor = 10;
  310.             }
  311.             else
  312.             {
  313.                 destFloor = std::stoi(destFloorIn);
  314.             }
  315.  
  316.             if(num == 1)
  317.             {
  318.                 oneDestList[oneCarrying + 1] = destFloor;
  319.                 oneCarrying++;
  320.             }
  321.             else
  322.             {
  323.                 twoDestList[twoCarrying + 1] = destFloor;
  324.                 twoCarrying++;
  325.             }
  326.         }
  327.         calls--;
  328.     }
  329. }
  330.  
  331. void dropOff()
  332. {
  333.     for(int i = 0; i < 10; i++)
  334.     {
  335.         if(oneDestList[i] == oneCurFloor)
  336.         {
  337.             oneDestList[i] = 0;
  338.             oneCarrying--;
  339.         }
  340.         if(twoDestList[i] == twoCurFloor)
  341.         {
  342.             twoDestList[i] = 0;
  343.             twoCarrying--;
  344.         }
  345.     }
  346.  
  347.     for(int i = 0; i < 10; i++)
  348.     {
  349.         if(oneDestList[i] == 0)
  350.         {
  351.             for(int j = i + 1; j < 10; j++)
  352.             {
  353.                 if(oneDestList[j] != 0)
  354.                 {
  355.                     oneDestList[i] = oneDestList[j];
  356.                     oneDestList[j] = 0;
  357.                 }
  358.             }
  359.         }
  360.  
  361.         if(twoDestList[i] == 0)
  362.         {
  363.             for(int j = i + 1; j < 10; j++)
  364.             {
  365.                 if(twoDestList[j] != 0)
  366.                 {
  367.                     twoDestList[i] = twoDestList[j];
  368.                     twoDestList[j] = 0;
  369.                 }
  370.             }
  371.         }
  372.     }
  373. }
  374.  
  375. void bringDown()
  376. {
  377.     for(int i = 0; i < 20; i++)
  378.     {
  379.         if(callList[i].empty())
  380.         {
  381.             for(int j = i + 1; j < 10; j++)
  382.             {
  383.                 if(!callList[j].empty())
  384.                 {
  385.                     callList[i] = callList[j];
  386.                     callList[j] = "";
  387.                 }
  388.             }
  389.         }
  390.         if(oneDestList[i % 10] == 0)
  391.         {
  392.             for(int j = i + 1; j < 10; j++)
  393.             {
  394.                 if(oneDestList[j] != 0)
  395.                 {
  396.                     oneDestList[i] = oneDestList[j];
  397.                     oneDestList[j] = 0;
  398.                 }
  399.             }
  400.         }
  401.         if(twoDestList[i % 10] == 0)
  402.         {
  403.             for(int j = i + 1; j < 10; j++)
  404.             {
  405.                 if(twoDestList[j] != 0)
  406.                 {
  407.                     twoDestList[i] = twoDestList[j];
  408.                     twoDestList[j] = 0;
  409.                 }
  410.             }
  411.         }
  412.     }
  413. }
  414.  
  415. void moveFloor(bool goingUp, int num)
  416. {
  417.     if(num == 1)
  418.     {
  419.         if(goingUp)
  420.         {
  421.             oneDir = "U";
  422.             oneCurFloor++;
  423.             if(oneCurFloor == 10)
  424.             {
  425.                 oneDir = "N";
  426.             }
  427.         }
  428.         else
  429.         {
  430.             oneDir = "D";
  431.             oneCurFloor--;
  432.             if(oneCurFloor == 1)
  433.             {
  434.                 oneDir = "N";
  435.             }
  436.         }
  437.         dropOff();
  438.         pickUp(1);
  439.     }
  440.     if(num == 2)
  441.     {
  442.         if(goingUp)
  443.         {
  444.             twoDir = "U";
  445.             twoCurFloor++;
  446.             if(twoCurFloor == 10)
  447.             {
  448.                 twoDir = "N";
  449.             }
  450.         }
  451.         else
  452.         {
  453.             twoDir = "D";
  454.             twoCurFloor--;
  455.             if(twoCurFloor == 1)
  456.             {
  457.                 twoDir = "N";
  458.             }
  459.         }
  460.         dropOff();
  461.         pickUp(2);
  462.     }
  463.     dropOff(); //covers a passenger picking the same floor as they're on
  464. }
  465.  
  466. void carryPassengers(int num)
  467. {
  468.     int destFloor = 100;
  469.     for(int i = 0; i < 10; i++)
  470.     {
  471.         int cur;
  472.         if(num == 1)
  473.         {
  474.             cur = oneDestList[i];
  475.             if(abs(oneCurFloor - cur) < abs(oneCurFloor - destFloor))
  476.             {
  477.                 destFloor = cur;
  478.             }
  479.         }
  480.         else
  481.         {
  482.             cur = twoDestList[i];
  483.             if(abs(twoCurFloor - cur) < abs(twoCurFloor - destFloor))
  484.             {
  485.                 destFloor = cur;
  486.             }
  487.         }
  488.     }
  489.     moveFloor((num == 1 && oneCurFloor < destFloor) || (num == 2 && twoCurFloor < destFloor), num);
  490. }
  491.  
  492. int main()
  493. {
  494.     printElevator(1, false, false, 1, false, false);
  495.     oneDir = "N";
  496.     twoDir = "N";
  497.  
  498.     while(true) {
  499.         int rtn = newInput();
  500.         if (rtn == 0) {
  501.             std::cout << "Have a nice day!" << std::endl;
  502.             return 0;
  503.         }
  504.  
  505.         bringDown();
  506.  
  507.         std::cout << "Calls: " + std::to_string(calls) << std::endl;
  508.         std::cout << "Elevator One: Carrying " + std::to_string(oneCarrying) << std::endl;
  509.         std::cout << "Elevator Two: Carrying " + std::to_string(twoCarrying) << std::endl;
  510.  
  511.         //elevator procedure
  512.         if(calls == 0)
  513.         {
  514.             if(oneCurFloor > 1 && oneCarrying == 0)
  515.             {
  516.                 moveFloor(false, 1);
  517.             }
  518.             else if(oneCarrying > 0)
  519.             {
  520.                 carryPassengers(1);
  521.             }
  522.             if(twoCurFloor > 1 && twoCarrying == 0)
  523.             {
  524.                 moveFloor(false, 2);
  525.             }
  526.             else if(twoCarrying > 0)
  527.             {
  528.                 carryPassengers(2);
  529.             }
  530.         }
  531.         else if(oneCarrying == 0)
  532.         {
  533.             std::cout << "Send One to the nearest call." << std::endl;
  534.             if(twoCarrying == 0)
  535.             {
  536.                 //send One to the nearest call
  537.                 int oneTargetFloor = 0;
  538.                 int oneLowestDist = 100;
  539.                 int oneTargetIndex = -1;
  540.                 int twoTargetFloor = 0;
  541.                 int twoLowestDist = 100;
  542.                 for(int i = 0; i < 20; i++)
  543.                 {
  544.                     std::string cur = callList[i];
  545.                     int callFloor;
  546.                     if(cur[0] == 'R')
  547.                     {
  548.                         callFloor = 10;
  549.                     }
  550.                     else
  551.                     {
  552.                         callFloor = cur[0] - 48;
  553.                     }
  554.                     if(abs(oneCurFloor - callFloor) < oneLowestDist)
  555.                     {
  556.                         oneTargetFloor = callFloor;
  557.                         oneLowestDist = abs(oneCurFloor - callFloor);
  558.                         oneTargetIndex = i;
  559.                     }
  560.                 }
  561.                 if(calls == 1)
  562.                 {
  563.                     if(twoCurFloor > 1)
  564.                     {
  565.                         moveFloor(false, 2);
  566.                     }
  567.                 }
  568.                 else
  569.                 {
  570.                     //UGH... Send Two to the other call.
  571.                     for(int j = 0; j < 20; j++)
  572.                     {
  573.                         if(j != oneTargetIndex)
  574.                         {
  575.                             std::string cur = callList[j];
  576.                             int callFloor;
  577.                             if(cur[0] == 'R')
  578.                             {
  579.                                 callFloor = 10;
  580.                             }
  581.                             else
  582.                             {
  583.                                 callFloor = cur[0] - 48;
  584.                             }
  585.                             if(abs(twoCurFloor - callFloor) < twoLowestDist)
  586.                             {
  587.                                 twoTargetFloor = callFloor;
  588.                                 twoLowestDist = abs(twoCurFloor - callFloor);
  589.                             }
  590.                         }
  591.                     }
  592.                 }
  593.                 if(oneTargetFloor > oneCurFloor)
  594.                 {
  595.                     moveFloor(true, 1);
  596.                 }
  597.                 else if(oneTargetFloor < oneCurFloor)
  598.                 {
  599.                     moveFloor(false, 1);
  600.                 }
  601.                 if(twoTargetFloor > twoCurFloor && twoCurFloor != 10)
  602.                 {
  603.                     moveFloor(true, 2);
  604.                 }
  605.                 else if(twoTargetFloor < twoCurFloor && twoCurFloor != 1)
  606.                 {
  607.                     moveFloor(false, 2);
  608.                 }
  609.             }
  610.             else
  611.             {
  612.                 //send One (empty) to the nearest call not in Two's direction, if there is one
  613.                 int oneTarget;
  614.                 int targetDist = 100;
  615.                 for(int i = 0; i < 20; i++)
  616.                 {
  617.                     std::string curCall = callList[i];
  618.                     int curCallFloor;
  619.                     if(!curCall.empty())
  620.                     {
  621.                         if(curCall[0] == 'R')
  622.                         {
  623.                             curCallFloor = 10;
  624.                         }
  625.                         else
  626.                         {
  627.                             curCallFloor = curCall[0] - 48;
  628.                         }
  629.  
  630.                         if(curCall[1] != twoDir[0] && curCallFloor - oneCurFloor < targetDist)
  631.                         {
  632.                             oneTarget = curCallFloor;
  633.                             targetDist = curCallFloor - oneCurFloor;
  634.                         }
  635.                     }
  636.                 }
  637.                 if(targetDist == 100)
  638.                 {
  639.                     //send One to ground floor if it's not already there - Two can pick up the calls
  640.                     if(oneCurFloor > 1)
  641.                     {
  642.                         moveFloor(false, 1);
  643.                     }
  644.                 }
  645.                 else
  646.                 {
  647.                     moveFloor(oneTarget > oneCurFloor, 1);
  648.                 }
  649.                 carryPassengers(2);
  650.             }
  651.         }
  652.         else if(twoCarrying == 0)
  653.         {
  654.             //send Two (empty) to the nearest call not in One's direction, if there is one
  655.             int twoTarget;
  656.             int targetDist = 100;
  657.             for(int i = 0; i < 20; i++)
  658.             {
  659.                 std::string curCall = callList[i];
  660.                 int curCallFloor;
  661.                 if(!curCall.empty())
  662.                 {
  663.                     if(curCall[0] == 'R')
  664.                     {
  665.                         curCallFloor = 10;
  666.                     }
  667.                     else
  668.                     {
  669.                         curCallFloor = curCall[0] - 48;
  670.                     }
  671.                     if(curCall[1] != oneDir[0] && curCallFloor - twoCurFloor < targetDist)
  672.                     {
  673.                         twoTarget = curCallFloor;
  674.                         targetDist = curCallFloor - twoCurFloor;
  675.                     }
  676.                 }
  677.             }
  678.             if(targetDist == 100)
  679.             {
  680.                 //send Two to ground floor if it's not already there - One can pick up the calls
  681.                 if(twoCurFloor > 1)
  682.                 {
  683.                     moveFloor(false, 2);
  684.                 }
  685.             }
  686.             else
  687.             {
  688.                 moveFloor(twoTarget > twoCurFloor, 1);
  689.             }
  690.             carryPassengers(1);
  691.         }
  692.         else
  693.         {
  694.             //pick up passengers if they're on the way, otherwise just keep going
  695.             carryPassengers(1);
  696.             carryPassengers(2);
  697.         }
  698.         printElevator(oneCurFloor, oneDir != "N", oneDir == "U", twoCurFloor, twoDir != "N", twoDir == "U");
  699.     }
  700. }
Advertisement
Add Comment
Please, Sign In to add comment