SHARE
TWEET

Untitled

a guest Feb 14th, 2020 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input, bomb){
  2.     let bombNumber = Number(bomb[0]);
  3.     let bombRange = Number(bomb[1]);
  4.     let arr = input.map(Number);
  5.     let reduced = 0;
  6.     while(arr.includes(bombNumber)){
  7.            let position= arr.indexOf(bombNumber);
  8.             if(position - bombRange < 0 && position + bombRange > arr.length){
  9.                 arr = [];
  10.             }else if(position - bombRange < 0){
  11.                 arr.splice(0, 1 + position + bombRange);
  12.             }else if(position + bombRange >= arr.length){
  13.                 arr.splice(position - bombRange, bombRange + arr.length  - position)
  14.             }else{
  15.                 arr.splice(position - bombRange, 2 * bombRange + 1);
  16.             }
  17.        }
  18.        for(let i = 0; i < arr.length; i++){
  19.            if(arr.length === 1){
  20.                reduced = arr[0]
  21.            }else{
  22.                reduced = arr.reduce((a, b) => a + b);
  23.            }
  24.     }
  25.    console.log(reduced);
  26. }
  27. solve([1, 2, 2, 4, 2, 2, 2, 9],
  28.     [4, 2]);
  29. solve([1, 4, 4, 2, 8, 9, 1],
  30.     [9, 3]);
  31. solve([1, 7, 7, 1, 2, 3],
  32.     [7, 1]);
  33. solve([1, 1, 2, 1, 1, 1, 2, 1, 1, 1],
  34.     [2, 1]);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top