Advertisement
-Annie-

EXAM-04.Airport

Jun 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(dataRows) {
  2.     let airport = new Set();
  3.     let townStatistics = new Map();
  4.     let townPlanes = new Map();
  5.  
  6.     for(let dataRow of dataRows) {
  7.         let [planeId, town, passengersCount, action]
  8.             = dataRow.split(/\s+/g);
  9.         passengersCount = Number(passengersCount);
  10.  
  11.         if(action === 'land'){
  12.             if (airport.has(planeId)) {
  13.                 continue;
  14.             } else {
  15.                 airport.add(planeId);
  16.             }
  17.  
  18.             if(!townStatistics.has(town)){
  19.                 townStatistics.set(town, [0, 0]);
  20.             }
  21.  
  22.             if(!townPlanes.has(town)){
  23.                 townPlanes.set(town, new Set());
  24.             }
  25.  
  26.             townStatistics.get(town)[0] += passengersCount;
  27.             townPlanes.get(town).add(planeId);
  28.         } else {
  29.             if(airport.has(planeId)){
  30.                 airport.delete(planeId);
  31.             } else {
  32.                 continue;
  33.             }
  34.  
  35.             if(!townStatistics.has(town)){
  36.                 townStatistics.set(town, [0, 0]);
  37.             }
  38.  
  39.             if(!townPlanes.has(town)){
  40.                 townPlanes.set(town, new Set());
  41.             }
  42.  
  43.             townStatistics.get(town)[1] += passengersCount;
  44.             townPlanes.get(town).add(planeId);
  45.         }
  46.     }
  47.  
  48.     let sortedAirport = Array.from(airport.values())
  49.         .sort((a, b) => a.localeCompare(b));
  50.     console.log('Planes left:');
  51.     for(let planeId of sortedAirport) {
  52.         console.log(`- ${planeId}`);
  53.     }
  54.     let sortedTowns = [...townStatistics.entries()]
  55.         .sort(sortTowns);
  56.     for(let [town, statistics] of sortedTowns) {
  57.         console.log(town);
  58.         console.log(`Arrivals: ${statistics[0]}`);
  59.         console.log(`Departures: ${statistics[1]}`);
  60.         let sortedPlanes = [...townPlanes.get(town).values()]
  61.             .sort((a, b) => a.localeCompare(b));
  62.         console.log('Planes:');
  63.         for(let planeId of sortedPlanes) {
  64.             console.log(`-- ${planeId}`);
  65.         }
  66.     }
  67.  
  68.     function sortTowns(a, b) {
  69.         let aArrivals = a[1][0];
  70.         let bArrivals = b[1][0];
  71.         let firstCriteria = bArrivals - aArrivals;
  72.  
  73.         if(firstCriteria !== 0){
  74.             return firstCriteria;
  75.         } else {
  76.             return a[0].localeCompare(b[0]);
  77.         }
  78.     }
  79. }
  80.  
  81. solve([ "Airbus Paris 356 land",
  82.     "Airbus London 321 land",
  83.     "Airbus Paris 213 depart",
  84.     "Airbus Ljubljana 250 land"
  85. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement