audipower

concert

Jul 24th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function concert(input) {
  2.  
  3.     let time = {};
  4.     let list = {};
  5.     let totalTime = 0;
  6.  
  7.     for (let line of input) {
  8.         if (line === "start of concert") {
  9.             break;
  10.         }
  11.         let [command, band, timeOrMembers] = line.split("; ");
  12.        
  13.         if (command === "Add") {
  14.             timeOrMembers = timeOrMembers.split(", ");
  15.             if (!list.hasOwnProperty(band)) {
  16.                 list[band] = new Set();
  17.             }
  18.             timeOrMembers.forEach((member) => list[band].add(member))
  19.         } else if (command === "Play") {
  20.             timeOrMembers = Number(timeOrMembers);
  21.             totalTime += timeOrMembers;
  22.             if (!time.hasOwnProperty(band)) {
  23.                 time[band] = 0;
  24.             }
  25.             time[band] += timeOrMembers;
  26.         }
  27.     }
  28.  
  29.     console.log(`Total time: ${totalTime}`);
  30.     let entries = Object.entries(time);
  31.     entries.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).forEach((tuple) => console.log(`${tuple[0]} -> ${tuple[1]}`))
  32.    
  33.     let bandToPrint = input.pop();
  34.     console.log(bandToPrint)
  35.     list[bandToPrint].forEach(member => console.log(`=> ${member}`))
  36. }
Advertisement
Add Comment
Please, Sign In to add comment