svephoto

The Lift [JavaScript]

Oct 1st, 2021 (edited)
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let peopleInQueue = Number(input.shift());
  3.     let wagons = input.toString().split(" ").map((x)=> Number(x));
  4.  
  5.     for (let i = 0; i < wagons.length; i++) {
  6.         if (wagons[i] < 4) {
  7.             if (peopleInQueue >= 4 - wagons[i]) {
  8.                 peopleInQueue -= 4 - wagons[i];
  9.                 wagons[i] = 4;
  10.             } else {
  11.                 wagons[i] += peopleInQueue;
  12.                 peopleInQueue = 0;
  13.             }
  14.         }
  15.     }
  16.  
  17.     let isNotFull = false;
  18.  
  19.     for (let i = 0; i < wagons.length; i++) {
  20.         let currenEl = wagons[i];
  21.  
  22.         if (currenEl < 4) {
  23.             isNotFull = true;
  24.         }
  25.     }
  26.  
  27.     if (!isNotFull && peopleInQueue > 0) {
  28.         console.log(`There isn't enough space! ${peopleInQueue} people in a queue!`);
  29.    }
  30.  
  31.    if (isNotFull && peopleInQueue == 0) {
  32.        console.log("The lift has empty spots!");
  33.    }
  34.  
  35.    console.log(wagons.join(" "));
  36. }
  37.  
RAW Paste Data