Advertisement
Guest User

02. Bunny Kill

a guest
Jan 31st, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function massacre(arr) {
  2.     let bunnyBombs = arr.pop()
  3.                     .split(" ")
  4.                     .map(element => element.split(",")
  5.                                             .map(Number));
  6.  
  7.     let field = arr.map(row => row.split(' ')
  8.                                   .map(Number));
  9.  
  10.     let snowRamboDemmage = 0;
  11.     let bunnyesMassacred = 0;
  12.  
  13.     // Dealing dammage for each bombbunny + addind bomb bunny bomb power
  14.     bunnyBombs.forEach(bunny => {
  15.        
  16.         let row = bunny[0];
  17.         let col = bunny[1];
  18.  
  19.         // Defining bomb bunnie bomb power
  20.         bombBunny = field[col][row];
  21.        
  22.         //dealing dammage by rows + check for KIA bomb bunnies (bombed bombs)
  23.         if (bombBunny > 0) {
  24.             snowRamboDemmage += bombBunny;
  25.             bunnyesMassacred += 1;
  26.             // Checking positions around the bomb
  27.             for (let i = row - 1; i <= row + 1; i++) {
  28.                 for (let j = col - 1; j <= col + 1; j++) {
  29.                     // Check if the position is outside the board
  30.                     if (j >= 0 && i >= 0 && j <= field.length - 1 && i <= field[0].length) {
  31.                         field[i][j] = field[i][j] - bombBunny;
  32.                         if (field[i][j] < 0) {
  33.                             field[i][j] = 0;
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     });
  40.     // Killing the bunnies that susvived IIWW
  41.     field.forEach(row => {
  42.         for (const cell in row) {
  43.            
  44.             if (row[cell] > 0) {
  45.                 bunnyesMassacred++;
  46.                 snowRamboDemmage += row[cell];
  47.             }
  48.         }
  49.     });
  50.     console.log(snowRamboDemmage)
  51.     console.log(bunnyesMassacred)
  52. }
  53.  
  54. massacre(
  55.     ['5 10 15 20',
  56. '10 10 10 10',
  57. '10 15 10 10',
  58. '10 10 10 10',
  59. '2,2 0,1']
  60. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement