Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bunnyKill(arr) {
- let bombCells = arr.pop();
- let currentCells = bombCells.split(' ');
- let sum = 0;
- let killCounter = 0;
- for (let i = 0; i < arr.length; i++) {
- arr[i] = arr[i].split(' ').map(x => Number(x));
- }
- for (let i = 0; i < currentCells.length; i++) {
- let bombRow = Number(currentCells[i][0]);
- let bombCol = Number(currentCells[i][2]);
- let bombValue = Number(arr[bombRow][bombCol]);
- killCounter++;
- for (let row = 0; row < arr.length; row++) {
- for (let col = 0; col < arr[row].length; col++) {
- if (bombValue > 0 && bombRow === row && bombCol === col) {
- let start = Math.max(0, bombCol - 1);
- let end = Math.min(bombCol + 1, arr[row].length - 1);
- let minRow = Math.max(0, row - 1);
- let maxRow = Math.min(arr[row].length, row + 1);
- for (let j = start; j <= end; j++) {
- if (minRow < row) {
- if ((arr[minRow][j] - bombValue) < 0) {
- arr[minRow][j] = Math.max(0, arr[minRow][j] - bombValue);
- } else {
- arr[minRow][j] = arr[minRow][j] - bombValue;
- }
- }
- if ((arr[row][j] - (bombValue)) < 0) {
- arr[row][j] = Math.max(0, arr[row][j] - (bombValue));
- } else {
- arr[row][j] = arr[row][j] - (bombValue);
- }
- if (maxRow > row) {
- if ((arr[maxRow][j] - (bombValue)) < 0) {
- arr[maxRow][j] = Math.max(0, arr[maxRow][j] - (bombValue));
- } else {
- arr[maxRow][j] = arr[maxRow][j] - (bombValue);
- }
- }
- }
- }
- }
- }
- sum += bombValue;
- }
- for (let i = 0; i < arr.length; i++) {
- for (let j = 0; j < arr[i].length; j++) {
- sum += arr[i][j];
- if (arr[i][j] !== 0) {
- killCounter++;
- }
- }
- }
- console.log(sum);
- console.log(killCounter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement