Advertisement
Spocoman

02. Bunny Kill

Dec 13th, 2022 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bunnyKill(input){
  2.     let bunnyKilled = 0;
  3.     let snowballDamage = 0;
  4.     let bombLocation = input.pop().split(' ');
  5.     let matrix = input.map(el => el = el.split(' ').map(Number));
  6.    
  7.     while(bombLocation.length > 0){
  8.         let [bombRow, bombCol] = bombLocation.shift().split(',').map(Number);
  9.         let bombValue = matrix[bombRow][bombCol];
  10.        
  11.         if (bombValue > 0){
  12.             bunnyKilled++;
  13.             snowballDamage += bombValue;
  14.  
  15.             for (let i = bombRow - 1; i <= bombRow + 1; i++){
  16.                 for (let j = bombCol - 1; j <= bombCol + 1; j++){
  17.                     if (i >= 0 && i < matrix.length && j >= 0 && j < matrix[i].length){
  18.                     matrix[i][j] -= matrix[i][j] > bombValue ? bombValue : matrix[i][j];
  19.                     }
  20.                 }
  21.                 matrix[bombRow][bombCol] = 0;      
  22.             }
  23.         }
  24.     }
  25.     let matrixToArray = matrix.flat().filter(x => x > 0);
  26.     snowballDamage += matrixToArray.reduce((a, b) => a + b);
  27.     bunnyKilled += matrixToArray.length;
  28.    
  29.     console.log(`${snowballDamage}\n${bunnyKilled}`);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement