desito07

Train

Jun 22nd, 2020
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. function train(input) {
  2. let numbers = input
  3. .shift()
  4. .split(" ")
  5. .map((x) => Number(x));
  6. // console.log(numbers);
  7.  
  8. let max = Number(input.shift()); // for each wagon in the train
  9. // console.log(max);
  10.  
  11. for (let i = 0; i < input.length; i++) {
  12. let arrNew = input[i].split(" ");
  13. let command = arrNew[0];
  14. let firstValue = arrNew[1];
  15.  
  16. // console.log(arrNew);
  17. // console.log(command);
  18. // console.log(firstValue);
  19. if (arrNew.length === 2 && command === "Add") {
  20. numbers.push(firstValue);
  21. } else {
  22. let passengers = Number(arrNew[0]);
  23. for (let j = 0; j < numbers.length; j++) {
  24. if (passengers + numbers[j] <= max);
  25. numbers[j] += passengers;
  26. break;
  27. }
  28. }
  29. }
  30. console.log(numbers.join(" "));
  31. }
  32. train(["32 54 21 12 4 0 23", "75", "Add 10", "Add 0", "30", "10", "75"]);
Add Comment
Please, Sign In to add comment