Advertisement
Guest User

Travellers Log

a guest
Oct 11th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function travellers(input) {
  2.  
  3.     let travellers = new Map();
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.         let data = input[i];
  7.  
  8.         if (data.match(/\w+ gets \d+/)) {
  9.             data = data.split(" gets ");
  10.  
  11.             let name = data[0];
  12.             let money = +data[1];
  13.  
  14.             if (!travellers.has(name)) {
  15.                 travellers.set(name, {
  16.                     name: name,
  17.                     country: new Map(),
  18.                     money: 0
  19.                 })
  20.             }
  21.  
  22.             travellers.get(name).money += money;
  23.         } else {
  24.             data = data.match(/(\w+) visited the (\w+) in (\w+) - (\d+)/);
  25.  
  26.             let name = data[1];
  27.             let landmark = data[2];
  28.             let country = data[3];
  29.             let cost = +data[4];
  30.  
  31.             if (!travellers.has(name)) {
  32.                 console.log(`Not enough money to visit ${landmark}`);
  33.             } else if (travellers.get(name).money < cost) {
  34.                 console.log(`Not enough money to visit ${landmark}`);
  35.             } else {
  36.                 if (!travellers.get(name).country.has(country)) {
  37.                     travellers.get(name).country.set(country, []);
  38.                 }
  39.  
  40.                 if (!travellers.get(name).country.get(country).includes(landmark)) {
  41.                     travellers.get(name).country.get(country).push(landmark);
  42.                     travellers.get(name).money -= cost;
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     let sortedTravellers = Array.from(travellers.values())
  49.         .sort((a, b) => b.country.size - a.country.size);
  50.  
  51.     for (let traveller of sortedTravellers) {
  52.         console.log(`${traveller.name} visited ${traveller.country.size} countries and has ${traveller.money} money left`);
  53.  
  54.         let sortedCountries = Array.from(traveller.country)
  55.             .sort((a, b) => b[1].length - a[1].length);
  56.  
  57.         for (let [country, landmarks] of sortedCountries) {
  58.             console.log(`- ${country} -> ${landmarks.length} landmarks`);
  59.  
  60.             let sortedLandmarks = landmarks.sort();
  61.  
  62.             for (let landmark of sortedLandmarks) {
  63.                 console.log(`-- ${landmark}`);
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69. travellers(['Peter gets 100',
  70.     'Peter visited the StatueOfLiberty in USA - 50',
  71.     'Bill gets 250',
  72.     'Tim visited the ChristTheRedeemer in Brazil - 150',
  73.     'Bill gets 400',
  74.     'Bill visited the MountFuji in Japan - 600',
  75.     'Bill visited the TeatroAmazonas in Brazil - 50',
  76.     'Bill gets 150',
  77.     'Bill visited the ChristTheRedeemer in Brazil - 150',
  78.     'Tim gets 500',
  79.     'Bill visited the StatueOfLiberty in USA - 440',
  80.     'Tim visited the StatueOfLiberty in USA - 440',
  81.     'Maria gets 650',
  82.     'Maria visited the StatueOfLiberty in USA - 440',
  83.     'Maria visited the CapeCod in USA - 100']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement