Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bunnyKill(input){
- let bunnyKilled = 0;
- let snowballDamage = 0;
- let bombLocation = input.pop().split(' ');
- let matrix = input.map(el => el = el.split(' ').map(Number));
- while(bombLocation.length > 0){
- let [bombRow, bombCol] = bombLocation.shift().split(',').map(Number);
- let bombValue = matrix[bombRow][bombCol];
- if (bombValue > 0){
- bunnyKilled++;
- snowballDamage += bombValue;
- for (let i = bombRow - 1; i <= bombRow + 1; i++){
- for (let j = bombCol - 1; j <= bombCol + 1; j++){
- if (i >= 0 && i < matrix.length && j >= 0 && j < matrix[i].length){
- matrix[i][j] -= matrix[i][j] > bombValue ? bombValue : matrix[i][j];
- }
- }
- matrix[bombRow][bombCol] = 0;
- }
- }
- }
- let matrixToArray = matrix.flat().filter(x => x > 0);
- snowballDamage += matrixToArray.reduce((a, b) => a + b);
- bunnyKilled += matrixToArray.length;
- console.log(`${snowballDamage}\n${bunnyKilled}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement