Ivankooo1

01.Concert

Mar 24th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (input = []) {
  2.     let addMembers = {};
  3.     let playTime = {};
  4.     let totalTime = 0;
  5.     let bandNameToPRint = input.pop();
  6.  
  7.     for (let i = 0; i < input.length; i++) {
  8.         let currentLine = input[i];
  9.         if (currentLine === 'start of concert') {
  10.             console.log(`Total time: ${totalTime}`);
  11.  
  12.             let sortedByTime = Object.entries(playTime)
  13.                 .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
  14.  
  15.             for (let band of sortedByTime) {
  16.                 let [bandName, time] = band;
  17.  
  18.                 console.log(`${bandName} -> ${time}`);
  19.             }
  20.  
  21.             console.log(bandNameToPRint);
  22.  
  23.             let allMembers = addMembers[bandNameToPRint];
  24.  
  25.             for (let mem of allMembers) {
  26.                 console.log(`=> ${mem}`);
  27.             }
  28.  
  29.  
  30.         } else {
  31.             let [command, group, values] = currentLine.split('; ');
  32.  
  33.             if (command === 'Play') {
  34.                 let time = Number(values);
  35.  
  36.                 if (!playTime.hasOwnProperty(group)) {
  37.                     playTime[group] = 0;
  38.                 }
  39.                 playTime[group] += time;
  40.                 totalTime += time;
  41.             } else if (command === 'Add') {
  42.                 let members = values.split(', ');
  43.  
  44.                 if (!addMembers.hasOwnProperty(group)) {
  45.                     addMembers[group] = [];
  46.                 }
  47.  
  48.  
  49.                 for (let member of members) {
  50.  
  51.                     if (!addMembers[group].includes(member)) {
  52.                         addMembers[group].push(member);
  53.                     }
  54.  
  55.                 }
  56.             }
  57.  
  58.  
  59.  
  60.         }
  61.     }
  62. }
  63.  
  64. concert([
  65.    
  66.     'Play; The Beatles; 2000',
  67.     'Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr',
  68.     'Add; The Beatles; Ivan, John Lennon, Paul McCartney, George Harrison, Ringo Starr',
  69.     'Add; Eagles; Glenn Frey, Don Henley, Bernie Leadon, Randy Meisner',
  70.     'Play; Eagles; 1869',
  71.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards',
  72.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards, Bill Wyman, Charlie Watts, Ian Stewart',
  73.     'Play; The Rolling Stones; 4239',
  74.     'start of concert',
  75.     'The Rolling Stones'
  76.   ]
  77.   );
Add Comment
Please, Sign In to add comment