Advertisement
Guest User

Bunny Kill

a guest
May 27th, 2018
252
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.         for (let row = bombRow-1; row <= bombRow+1; row++) {
  20.             for (let col = bombCol-1; col <= bombCol+1; col++) {
  21.                 if((row>=0)&&(col>=0)&&(row < matrixRow)&&(col < matrixCol)){
  22.                     matrix[row][col] -= bombValue;
  23.                 }
  24.             }
  25.         }
  26.         killedBunnies++;
  27.         snowBallDamage += bombValue;
  28.     }
  29.     for (let row = 0; row < matrix.length; row++) {
  30.         for (let col = 0; col < matrix[row].length; col++) {
  31.             if(matrix[row][col]>0){
  32.                 snowBallDamage += matrix[row][col];
  33.                 killedBunnies++;
  34.             }
  35.         }
  36.     }
  37.     console.log(snowBallDamage);
  38.     console.log(killedBunnies);
  39. }
  40. solve(['10 100 100',
  41. '100 100 100',
  42. '100 100 100',
  43. '0,0 2,2']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement