Advertisement
dimoBs

shoot for the win

Feb 23rd, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //02. Shoot for the Win
  2. function solve(array) {
  3.  
  4.     let count = 0;
  5.     let aim = array.shift().split(' ').map(Number)
  6.     let command = array.shift();
  7.     let shoot = -1;
  8.  
  9.     while (command !== 'End') {
  10.         let index = Number(command);
  11.         if (index < aim.length && index >= 0) {
  12.             if (aim[index] === -1) {
  13.             } else {
  14.                 let currentNumber = aim[index];
  15.                 aim[index] = shoot;
  16.                 count++;
  17.                 for (let i = 0; i < aim.length; i++) {
  18.                     if (aim[i] > -1) {
  19.                         if (aim[i] > currentNumber) {
  20.                             aim[i] = aim[i] - currentNumber;
  21.                         } else {
  22.                             aim[i] = aim[i] + currentNumber;
  23.                         }
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.         command = array.shift();
  29.     }
  30.     console.log(`Shot targets: ${count} -> ${aim.join(' ')}`);
  31. }
  32. solve(['24 50 36 70', '0', '4', '3', '1', 'End']);
  33. solve(['30 30 12 60 54 66',5,2,4,0,'End']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement