svephoto

Shoot for the Win [JavaScript]

Jul 15th, 2021 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shootForTheWin(input) {
  2.     let targets = input.shift().split(" ").map(Number);
  3.  
  4.     let count = 0;
  5.     let command = input.shift();
  6.  
  7.     while (command !== "End") {
  8.         let indexOfTheTarget = Number.parseInt(command);
  9.  
  10.         if (indexOfTheTarget >= 0 && indexOfTheTarget < targets.length) {
  11.             for (let i = 0; i < targets.length; i++) {
  12.                 let currentTarget = targets[indexOfTheTarget];
  13.        
  14.                 if (i !== indexOfTheTarget && targets[i] !== -1) {
  15.                     if (targets[i] > currentTarget) {
  16.                         targets[i] -= currentTarget;
  17.                     } else {
  18.                         targets[i] += currentTarget;
  19.                     }
  20.                 }
  21.             }
  22.  
  23.             targets[indexOfTheTarget] = -1;
  24.             count++;
  25.         }
  26.  
  27.         command = input.shift();
  28.     }
  29.  
  30.     console.log(`Shot targets: ${count} -> ${targets.join(" ")}`);
  31. }
  32.  
Add Comment
Please, Sign In to add comment