ErolKZ

Untitled

Oct 19th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. function solve(arr) {
  2.  
  3. let targets = arr.shift().split(' ').map(Number);
  4.  
  5. let i = 0;
  6.  
  7. let values = '';
  8.  
  9. let shotTargets = [];
  10.  
  11. let shotIndexes = [];
  12.  
  13. let valueOfIndex = 0;
  14.  
  15. let arrIncrease = [];
  16.  
  17. // console.log(arr);
  18.  
  19.  
  20. while (arr[i] !== 'End') {
  21.  
  22. let index = Number(arr[i]);
  23.  
  24. // Keep in mind that you can't shoot a target, which is already shot. You also can't increase or reduce a target, which is considered shot.
  25.  
  26. if (index < targets.length && !shotIndexes.includes(targets.indexOf(targets[index]))) {
  27.  
  28. shotIndexes.push(index);
  29.  
  30. arrIncrease = targets.slice();
  31.  
  32. targets.forEach(num => num > targets[index] && !shotIndexes.includes(targets.indexOf(num)) ? targets[targets.indexOf(num)] -= targets[index] : false);
  33.  
  34. valueOfIndex = arrIncrease[index];
  35.  
  36. targets[index] = -1;
  37.  
  38. arrIncrease.forEach(num => num <= valueOfIndex && !shotIndexes.includes(targets.indexOf(num)) && targets.indexOf(num) !== -1 ? targets[targets.indexOf(num)] += valueOfIndex : false);
  39.  
  40.  
  41.  
  42. }
  43.  
  44. i++;
  45.  
  46. }
  47.  
  48. shotTargets = targets.filter(num => num === -1);
  49.  
  50. targets.forEach(num => values += `${num} `);
  51.  
  52. console.log(`Shot targets: ${shotTargets.length} -> ${values}`);
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment