Advertisement
nikolayneykov

Untitled

Apr 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.     let bands = {};
  3.     let totalTime = 0;
  4.  
  5.     for (let param of params) {
  6.         if (param === 'start of concert') {
  7.             break;
  8.         }
  9.  
  10.         let [command, bandName, args] = param.split('; ');
  11.  
  12.         if (!bands.hasOwnProperty(bandName)) {
  13.             bands[bandName] = {
  14.                 time: 0,
  15.                 members: new Set()
  16.             };
  17.         }
  18.  
  19.         if (command === 'Add') {
  20.             let members = args.split(', ');
  21.             members.forEach(m => bands[bandName].members.add(m));
  22.         } else if (command === 'Play') {
  23.             let time = Number(args);
  24.             totalTime += time;
  25.             bands[bandName].time += time;
  26.         }
  27.  
  28.     }
  29.  
  30.     console.log(`Total time: ${totalTime}`);
  31.  
  32.     Object.entries(bands).sort(function (a, b) {
  33.         let result = b[1].time - a[1].time;
  34.  
  35.         if (result === 0) {
  36.             result = a[0].localeCompare(b[0]);
  37.         }
  38.  
  39.         return result;
  40.     }).forEach(band => console.log(`${band[0]} -> ${band[1].time}`));
  41.  
  42.     let bandName = params[params.length-1];
  43.     console.log(bandName);
  44.     bands[bandName].members.forEach(m=>console.log(`=> ${m}`));
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement