georgiev955

02. Bunny Kill

Jun 3rd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bunnyKill(inputArray) {
  2.     let bombBunniesCoords = inputArray.pop().split(' ');
  3.     let inputArrayLength = inputArray.length - 1;
  4.     let matrix = [];
  5.     let killed = 0;
  6.     let totalDamage = 0;
  7.     while (inputArrayLength >= 0) {
  8.         let currentRow = inputArray.slice(0, 1);
  9.         inputArray.shift();
  10.         matrix.push(currentRow);
  11.         inputArrayLength--;
  12.     }
  13.  
  14.     matrix = matrix.map(row => row[0].split(' ').map(Number));
  15.      
  16.  
  17.     for (let i = 0; i < bombBunniesCoords.length; i++) {
  18.         let currentBombCoords = bombBunniesCoords[i].split(',');
  19.         let row = Number(currentBombCoords[0]);
  20.         let col = Number(currentBombCoords[1]);
  21.         let bunnyDamage = matrix[row][col];
  22.         if (bunnyDamage === 0) {
  23.             continue;
  24.         }
  25.         killed++;
  26.         totalDamage += bunnyDamage;
  27.         for (let rows = row - 1; rows <= row + 1; rows++) {
  28.             if (rows < 0 || rows > matrix.length - 1) {
  29.                 continue;
  30.             }
  31.             let currentLine = matrix[rows];
  32.             for (let cols = col - 1; cols <= col + 1; cols++) {
  33.                 if (cols < 0 || cols > matrix[rows].length - 1) {
  34.                     continue
  35.                 }
  36.                 let bunnyHealth = matrix[rows][cols];
  37.                 if (matrix[rows][cols] === 0) {
  38.                     continue;
  39.                 }
  40.                 if (bunnyHealth > bunnyDamage) {
  41.                     matrix[rows][cols] -= bunnyDamage;
  42.                 } else {
  43.                     matrix[rows][cols] = 0;
  44.                 }
  45.             }
  46.         }
  47.  
  48.     }
  49.     for (let rows = 0; rows < matrix.length; rows++) {
  50.         let currentRow = matrix[rows];
  51.         for (let cols = 0; cols < matrix[rows].length; cols++) {
  52.             let currentBunny = matrix[rows][cols];
  53.             if (currentBunny !== 0) {
  54.                 totalDamage += currentBunny;
  55.                 killed++;
  56.             }
  57.         }
  58.     }
  59.    
  60.     console.log(totalDamage);
  61.     console.log(killed);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment