Advertisement
Spocoman

01. Train

Feb 6th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function train(input) {
  2.     let arr = input.shift().split(' ').map(Number);
  3.     let capacity = Number(input.shift());
  4.  
  5.     while (input.length !== 0) {
  6.         let command = input.shift().split(' ');
  7.         command[0] === 'Add' ? arr.push(Number(command[1])) : command = Number(command[0]);
  8.  
  9.         for (let i = 0; i < arr.length; i++) {
  10.             if (arr[i] + command <= capacity) {
  11.                 arr[i] += command;
  12.                 break;
  13.             }
  14.         }
  15.     }
  16.     console.log(arr.join(' '));
  17.  
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement