Advertisement
Spocoman

03. Air Pollution

Dec 14th, 2022
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function airPollution(areas, commands) {
  2.     areas = areas.map(x => x.split(' ').map(Number));
  3.  
  4.     while (commands.length !== 0){
  5.         let [com, arg] = commands.shift().split(' ');
  6.         if (com === 'smog'){
  7.             areas = areas.map(x => x.map(i => i + Number(arg)));
  8.         } else {
  9.             let index = Number(arg);
  10.             if (com === 'breeze'){    
  11.                 for (let row = 0; row < areas.length; row++) {
  12.                     let number = areas[index][row];
  13.                     areas[index][row] -= number < 15 ? number : 15;      
  14.                 }
  15.             } else if (com === 'gale'){
  16.                 for (let col = 0; col < areas.length; col++) {
  17.                     let number = areas[col][index];
  18.                     areas[col][index] -= number < 20 ? number : 20;      
  19.                 }
  20.             }
  21.         }
  22.     }
  23.  
  24.     let pollutedAreas = [];
  25.  
  26.     for (let i = 0; i < areas.length; i++) {
  27.         for (let j = 0; j < areas[i].length; j++) {
  28.            if (areas[i][j] >= 50){
  29.             pollutedAreas.push(`[${i}-${j}]`);
  30.            }
  31.         }    
  32.     }
  33.  
  34.     if (pollutedAreas.length === 0){
  35.         console.log('No polluted areas');
  36.     } else {
  37.         console.log(`Polluted areas: ${pollutedAreas.join(', ')}`);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement