Advertisement
fbinnzhivko

Untitled

Dec 17th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(flights) {
  2.     const LAND = 'land';
  3.  
  4.     let landed = [];
  5.     let townsStats = new Map();
  6.  
  7.     for (let flight of flights) {
  8.         let [id, town, passengers, action] = flight.split(/\s+/g);
  9.  
  10.         if (action === LAND) {
  11.             if (landed.indexOf(id) >= 0)
  12.                 continue;
  13.  
  14.             landed.push(id);
  15.         } else {
  16.             if (landed.indexOf(id) < 0)
  17.                 continue;
  18.  
  19.             landed = landed.filter(p => p != id);
  20.         }
  21.  
  22.         if (!townsStats.has(town)) {
  23.             townsStats.set(town, {arrivals: 0, departures: 0, planes: new Set()});
  24.         }
  25.  
  26.         if (action === LAND)
  27.             townsStats.get(town).arrivals += Number(passengers);
  28.         else
  29.             townsStats.get(town).departures += Number(passengers);
  30.  
  31.         townsStats.get(town).planes.add(id);
  32.     }
  33.    
  34.     console.log('Planes left:');
  35.     let sorted = landed
  36.         .sort((p1, p2) => p1.localeCompare(p2));
  37.  
  38.     for (let p of sorted) {
  39.         console.log(`- ${p}`);
  40.     }
  41.  
  42.     let sortedTowns = [...townsStats.keys()]
  43.         .sort(function (t1, t2) {
  44.             let res = townsStats.get(t2).arrivals - townsStats.get(t1).arrivals;
  45.             if (res === 0) {
  46.                 res = t1.localeCompare(t2);
  47.             }
  48.  
  49.             return res;
  50.         });
  51.  
  52.     for (let town of sortedTowns) {
  53.         let currentTown = townsStats.get(town);
  54.  
  55.         console.log(town);
  56.         console.log(`Arrivals: ${currentTown.arrivals}`);
  57.         console.log(`Departures: ${currentTown.departures}`);
  58.         console.log('Planes:');
  59.         let sortedPlanes = [...currentTown.planes.values()]
  60.             .sort((p1, p2) => p1.localeCompare(p2));
  61.  
  62.         for (let p of sortedPlanes) {
  63.             console.log(`-- ${p}`);
  64.         }
  65.     }
  66. }
  67.  
  68. solve(["Boeing474 Madrid 300 land",
  69.     "AirForceOne WashingtonDC 178 land",
  70.     "Airbus London 265 depart",
  71.     "ATR72 WashingtonDC 272 land",
  72.     "ATR72 Madrid 135 depart"]);
  73.  
  74. solve([ "Airbus Paris 356 land",
  75.     "Airbus London 321 land",
  76.     "Airbus Paris 213 depart",
  77.     "Airbus Ljubljana 250 land"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement