Advertisement
simonradev

Airplanes

Feb 11th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function main(allFlights) {
  2.     function TownInfo() {
  3.         this.arrivals = 0;
  4.         this.departures = 0;
  5.         this.planes = new Set();
  6.     }
  7.  
  8.     const ActionLand = 'land';
  9.     const ActionDepart = 'depart';
  10.    
  11.     allFlights = allFlights.map(s => s.split(/\s+/g));
  12.  
  13.     const mapAirplanesByAction = new Map();
  14.     mapAirplanesByAction.set(ActionLand, new Set());
  15.     mapAirplanesByAction.set(ActionDepart, new Set());
  16.  
  17.     const mapTownInfosByTown = new Map();
  18.    
  19.     for (const [airplaneId, town, people, action] of allFlights) {
  20.         const parsedPeople = Number(people);
  21.  
  22.         if (airplaneCanLand(mapAirplanesByAction, action, airplaneId)) {
  23.             addAirplaneInAction(mapAirplanesByAction, ActionLand, airplaneId);
  24.             removeAirplaneFromAction(mapAirplanesByAction, ActionDepart, airplaneId);
  25.            
  26.             updateTownInfo(mapTownInfosByTown, town, parsedPeople, airplaneId, 'arrivals');
  27.  
  28.         } else if (airplaneCanDepart(mapAirplanesByAction, action, airplaneId)) {
  29.             addAirplaneInAction(mapAirplanesByAction, ActionDepart, airplaneId);
  30.             removeAirplaneFromAction(mapAirplanesByAction, ActionLand, airplaneId);
  31.  
  32.             updateTownInfo(mapTownInfosByTown, town, parsedPeople, airplaneId, 'departures');
  33.         }
  34.     }
  35.  
  36.     printResult(mapAirplanesByAction.get(ActionLand), mapTownInfosByTown);
  37.  
  38.     function printResult(setLandedPlanes, mapTowns) {
  39.         const arrayAirplanesId = getSortedArray(setLandedPlanes.keys(), sortAlphabetic);
  40.  
  41.         console.log('Planes left:');
  42.         for (const airplaneId of arrayAirplanesId) {
  43.             console.log(`- ${airplaneId}`);
  44.         }
  45.  
  46.         const arrayTownsEntries = getSortedArray(mapTowns.entries(), sortEntriesTowns);
  47.         for (const [town, townInfo] of arrayTownsEntries) {
  48.             console.log(town);
  49.             console.log(`Arrivals: ${townInfo.arrivals}`);
  50.             console.log(`Departures: ${townInfo.departures}`);
  51.             console.log('Planes:');
  52.  
  53.             const sortedPlanes = getSortedArray(townInfo.planes.keys(), sortAlphabetic);
  54.             for (const planeId of sortedPlanes) {
  55.                 console.log(`-- ${planeId}`);
  56.             }
  57.         }
  58.     }
  59.  
  60.     function getSortedArray(elements, sortingFunc) {
  61.         return Array.from(elements).sort(sortingFunc);
  62.     }
  63.  
  64.     function sortEntriesTowns(a, b) {
  65.         const arrivalsA = a[1].arrivals;
  66.         const arrivalsB = b[1].arrivals;
  67.  
  68.         if (arrivalsA > arrivalsB) {
  69.             return -1;
  70.         } else if (arrivalsA < arrivalsB) {
  71.             return 1;
  72.         }
  73.  
  74.         return sortAlphabetic(a[0], b[0]);
  75.     }
  76.  
  77.     function sortAlphabetic(a, b) {
  78.         a = a.toLowerCase();
  79.         b = b.toLowerCase();
  80.  
  81.         if (a > b) {
  82.             return 1;
  83.         } else if (a < b) {
  84.             return -1;
  85.         }
  86.  
  87.         return 0;
  88.     }
  89.  
  90.     function updateTownInfo(map, town, people, airplaneId, objectKey) {
  91.         if (!map.has(town)) {
  92.             map.set(town, new TownInfo());
  93.         }
  94.  
  95.         const townInfo = map.get(town);
  96.         townInfo[objectKey] += people;
  97.         townInfo.planes.add(airplaneId);
  98.     }
  99.  
  100.     function removeAirplaneFromAction(map, action, airplaneId) {
  101.         const airplanesForAction = map.get(action);
  102.         airplanesForAction.delete(airplaneId);
  103.     }
  104.  
  105.     function airplaneCanLand(map, action, airplaneId) {
  106.         return action === ActionLand &&
  107.                !airplaneIsInStatus(map, ActionLand, airplaneId);
  108.     }
  109.  
  110.     function airplaneCanDepart(map, action, airplaneId) {
  111.         return action === ActionDepart &&
  112.                !airplaneIsInStatus(map, ActionDepart, airplaneId) &&
  113.                airplaneIsInStatus(map, ActionLand, airplaneId);
  114.     }
  115.  
  116.     function addAirplaneInAction(map, action, airplaneId) {
  117.         const set = map.get(action);
  118.         set.add(airplaneId);
  119.     }
  120.  
  121.     function airplaneIsInStatus(map, action, airplaneId) {
  122.         const isInStatus = map.get(action).has(airplaneId);
  123.         return isInStatus;
  124.     }
  125. }
  126.  
  127. main([
  128.     'RTA72 London 140 land',
  129. 'RTA72 Brussels 240 depart',
  130. 'RTA72 Sofia 450 land',
  131. 'RTA72 Lisbon 240 depart',
  132. 'RTA72 Berlin 350 land',
  133. 'RTA72 Otava 201 depart',
  134. 'RTA72 Haga 350 land',
  135. 'RTA72 Otava 201 depart',
  136. 'RTA72 Dortmund 150 land',
  137. 'RTA72 Montana 243 depart',
  138. 'RTA72 Monreal 350 land',
  139. 'RTA72 NewYork 201 depart',
  140. 'RTA72 Pekin 350 land',
  141. 'RTA72 Tokyo 201 depart',
  142. 'RTA72 Warshaw 350 land',
  143. 'RTA72 Riga 201 depart',
  144.     ]
  145.     );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement