Advertisement
vladovip

Bunny Kill_JS Fund_ Arr Adv. More Ex

Feb 17th, 2022
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     'use strict';
  3.     let matrix = [];
  4.     let snowBallDamage = 0;
  5.     let killedBunnies = 0;
  6.     let matrixRow = arr.length - 1;
  7.     let matrixCol = arr[0].split(/\s+/).length;
  8.     let coor = arr[arr.length - 1]
  9.         .split(' ')
  10.         .filter(a => a !== '');
  11.     for (let i = 0; i < arr.length - 1; i++) {
  12.         matrix.push(arr[i].split(/\s+/).map(Number));
  13.     }
  14.     for (let i = 0; i < coor.length; i++) {
  15.         let innerArr = coor[i].split(/[,]/).filter(a => a !== '').map(Number);
  16.         let bombRow = Number(innerArr[0]);
  17.         let bombCol = Number(innerArr[1]);
  18.         let bombValue = matrix[bombRow][bombCol];
  19.         if (bombValue > 0) {
  20.             for (let row = bombRow - 1; row <= bombRow + 1; row++) {
  21.                 for (let col = bombCol - 1; col <= bombCol + 1; col++) {
  22.                     if ((row >= 0) && (col >= 0) && (row < matrixRow) && (col < matrixCol)) {
  23.                         matrix[row][col] -= bombValue;
  24.                     }
  25.                 }
  26.             }
  27.             killedBunnies++;
  28.             snowBallDamage += bombValue;
  29.         }
  30.     }
  31.     for (let row = 0; row < matrix.length; row++) {
  32.         for (let col = 0; col < matrix[row].length; col++) {
  33.             if (matrix[row][col] > 0) {
  34.                 snowBallDamage += matrix[row][col];
  35.                 killedBunnies++;
  36.             }
  37.         }
  38.     }
  39.     console.log(snowBallDamage);
  40.     console.log(killedBunnies);
  41. }
  42.  
  43. solve(['10 10 10',
  44. '10 10 10',
  45. '10 10 10',
  46. '0,0']);
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement