Guest User

Untitled

a guest
Jun 30th, 2020
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. function solve(input){
  2.  
  3. let targets = input.shift().split(' ').map(Number);
  4.  
  5. let length = input.length;
  6.  
  7. for (let i = 0; i < length; i++){
  8. let currentCommand = input.shift().split(' ');
  9. let action = currentCommand.shift();
  10. let currIndex = currentCommand.shift();
  11. currIndex = Number(currIndex);
  12.  
  13. if (action === 'Shoot'){
  14. let power = currentCommand.shift();
  15. power = Number(power);
  16.  
  17. for (let i = 0; i < targets.length; i++){
  18. if (i === currIndex){
  19. targets[i] -= power;
  20. if (targets[i] <= 0){
  21. targets.splice(i,1);
  22. }
  23. }
  24. }
  25.  
  26. } else if (action === 'Add'){
  27. let value = currentCommand.shift();
  28. value = Number(value);
  29. let isValid = false;
  30. for (let i = 0; i < targets.length; i++){
  31. if (i === currIndex){
  32. isValid = true;
  33. targets.splice(i,0,value);
  34. }
  35. }
  36. if (!isValid){
  37. console.log('Invalid placement!');
  38. }
  39.  
  40. } else if (action === 'Strike'){
  41. let radius = currentCommand.shift();
  42. radius = Number(radius);
  43. let isStrike = false;
  44.  
  45. for (let i = 0; i < targets.length; i++){
  46.  
  47. if (i === currIndex){
  48.  
  49. if ((i - radius !== -1 && i - radius !== undefined) && (radius+i !== -1 && radius+1 !== undefined)){
  50. targets.splice(i - radius,radius * 2 +1);
  51. isStrike = true;
  52. }
  53. }
  54. }
  55. if(!isStrike){
  56. console.log('Strike missed!');
  57. }
  58. }
  59. }
  60. console.log(targets.join('|'));
  61.  
  62. }
Add Comment
Please, Sign In to add comment