Advertisement
krustev_84

Untitled

Jun 10th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function bunnyKill(arr) {
  2.  
  3. let bombCells = arr.pop();
  4. let currentCells = bombCells.split(' ');
  5. let sum = 0;
  6. let killCounter = 0;
  7.  
  8. for (let i = 0; i < arr.length; i++) {
  9. arr[i] = arr[i].split(' ').map(x => Number(x));
  10. }
  11.  
  12. for (let i = 0; i < currentCells.length; i++) {
  13. let bombRow = Number(currentCells[i][0]);
  14. let bombCol = Number(currentCells[i][2]);
  15. let bombValue = Number(arr[bombRow][bombCol]);
  16. killCounter++;
  17.  
  18. for (let row = 0; row < arr.length; row++) {
  19.  
  20. for (let col = 0; col < arr[row].length; col++) {
  21.  
  22. if (bombValue > 0 && bombRow === row && bombCol === col) {
  23.  
  24. let start = Math.max(0, bombCol - 1);
  25. let end = Math.min(bombCol + 1, arr[row].length - 1);
  26.  
  27. let minRow = Math.max(0, row - 1);
  28. let maxRow = Math.min(arr[row].length, row + 1);
  29.  
  30. for (let j = start; j <= end; j++) {
  31.  
  32. if (minRow < row) {
  33. if ((arr[minRow][j] - bombValue) < 0) {
  34. arr[minRow][j] = Math.max(0, arr[minRow][j] - bombValue);
  35. } else {
  36. arr[minRow][j] = arr[minRow][j] - bombValue;
  37. }
  38. }
  39.  
  40. if ((arr[row][j] - (bombValue)) < 0) {
  41. arr[row][j] = Math.max(0, arr[row][j] - (bombValue));
  42. } else {
  43. arr[row][j] = arr[row][j] - (bombValue);
  44. }
  45.  
  46. if (maxRow > row) {
  47. if ((arr[maxRow][j] - (bombValue)) < 0) {
  48. arr[maxRow][j] = Math.max(0, arr[maxRow][j] - (bombValue));
  49. } else {
  50. arr[maxRow][j] = arr[maxRow][j] - (bombValue);
  51. }
  52. }
  53. }
  54.  
  55. }
  56.  
  57.  
  58. }
  59. }
  60.  
  61. sum += bombValue;
  62. }
  63.  
  64. for (let i = 0; i < arr.length; i++) {
  65. for (let j = 0; j < arr[i].length; j++) {
  66. sum += arr[i][j];
  67. if (arr[i][j] !== 0) {
  68. killCounter++;
  69. }
  70. }
  71. }
  72.  
  73. console.log(sum);
  74. console.log(killCounter);
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement