Advertisement
Prohause

AirPolution

Jun 9th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. function pollution(input, forces) {
  2. let map = [];
  3. for (let i = 0; i < input.length; i++) {
  4. map.push(input[i].split(' ').map(Number));
  5. }
  6.  
  7. function setBreeze(map, index) {
  8. for (let i = 0; i < map.length; i++) {
  9. map[index][i] -= 15;
  10. if (map[index][i] < 0) {
  11. map[index][i] = 0;
  12. }
  13. }
  14. }
  15.  
  16. function setGale(map, index) {
  17. for (let i = 0; i < map.length; i++) {
  18. map[i][index] -= 20;
  19. if (map[i][index] < 0) {
  20. map[i][index] = 0;
  21. }
  22. }
  23. }
  24.  
  25. function setSmog(map, index) {
  26. for (let i = 0; i < map.length; i++) {
  27. for (let j = 0; j < map[i].length; j++) {
  28. map[i][j] += index;
  29. }
  30. }
  31. }
  32.  
  33. for (let i = 0; i < forces.length; i++) {
  34. let tokens = forces[i].split(' ');
  35. let currentForce = tokens[0];
  36. let index = Number(tokens[1]);
  37.  
  38. switch (currentForce) {
  39. case 'breeze':
  40. setBreeze(map, index);
  41. break;
  42. case 'gale':
  43. setGale(map, index);
  44. break;
  45. case 'smog':
  46. setSmog(map, index);
  47. break;
  48. }
  49.  
  50. }
  51.  
  52. if (map.some(x => x.some(y => y >= 50))) {
  53. let polluted = [];
  54. for (let i = 0; i < map.length; i++) {
  55. for (let j = 0; j < map[i].length; j++) {
  56. if (map[i][j] >= 50) {
  57. polluted.push(`[${i}-${j}]`);
  58. }
  59. }
  60. }
  61. console.log(`Polluted areas: ${polluted.join(', ')}`);
  62.  
  63. }
  64. else {
  65. console.log('No polluted areas');
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement