Advertisement
t_sh0w

02. Shoot For The Win

Apr 7th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.   let targets = input.shift().split(" ").map(Number);
  3.   input.pop();
  4.  
  5.   for (let i = 0; i < input.length; i++) {
  6.     let currentTargetIndex = Number(input[i]);
  7.     if (0 <= currentTargetIndex && currentTargetIndex <= targets.length - 1) {
  8.       if (targets[currentTargetIndex] !== -1) {
  9.         for (let j = 0; j < targets.length; j++) {
  10.           if (targets[currentTargetIndex] < targets[j] && targets[j] !== -1) {
  11.             targets[j] -= targets[currentTargetIndex];
  12.           } else if (targets[currentTargetIndex] >= targets[j] && j !== currentTargetIndex && targets[j] !== -1) {
  13.             targets[j] += targets[currentTargetIndex];
  14.           }
  15.         }
  16.         targets.splice(currentTargetIndex, 1, -1);
  17.       }
  18.     }
  19.   }
  20.   let shotTargetsCounter = 0;
  21.  
  22.   for (const currentTarget of targets) {
  23.     if (currentTarget === -1) {
  24.       shotTargetsCounter++;
  25.     }
  26.   }
  27.   console.log(`Shot targets: ${shotTargetsCounter} -> ${targets.join(" ")}`);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement