Advertisement
Guest User

Untitled

a guest
Jul 31st, 2019
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.40 KB | None
  1. // On the Way to Annapurna
  2. function solve(input) {
  3.     let stores = {};
  4.  
  5.     const add = (storeName, items) => {
  6.         if (!stores.hasOwnProperty(storeName)) {
  7.             stores[storeName] = items
  8.         } else {
  9.             stores[storeName] += ',' + items;
  10.         }
  11.     };
  12.  
  13.     const remove = (storeName) => {
  14.         if (stores.hasOwnProperty(storeName)) {
  15.             delete stores[storeName];
  16.         }
  17.     };
  18.  
  19.     let cmdParser = {
  20.         'Add': add,
  21.         'Remove': remove
  22.     };
  23.  
  24.     for (let line of input) {
  25.         if (line === 'END') {
  26.            break;
  27.         }
  28.         let [ cmd, store, items] = line.split('->');
  29.  
  30.         cmdParser[cmd](store, items);
  31.     }
  32.  
  33.     console.log('Stores list:');
  34.  
  35.     let sortedByCount = Object.entries(stores).sort((a, b) => b[1].length - a[1].length && b[0].localeCompare(a[0]));
  36.     for (let tuple of sortedByCount) {
  37.         let store = tuple.shift();
  38.         console.log(store);
  39.         let items = tuple.join(',').split(',');
  40.         items.forEach((item) => {
  41.             console.log(`<<${item}>>`);
  42.         });
  43.  
  44.     }
  45. }
  46.  
  47. solve(
  48.     [
  49.         'Add->PeakSports->Map,Navigation,Compass',
  50.         'Add->Paragon->Sunscreen',
  51.         'Add->Groceries->Dried-fruit,Nuts',
  52.         'Add->Groceries->Nuts',
  53.         'Add->Paragon->Tent',
  54.         'Remove->Paragon',
  55.         'Add->Pharmacy->Pain-killers',
  56.         'END'
  57.     ]
  58. );
Advertisement
RAW Paste Data Copied
Advertisement