Advertisement
Guest User

Untitled

a guest
Jul 25th, 2019
304
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3. let finalBand = input.pop();
  4. input.pop();
  5. let concert = {};
  6. let totalTime = 0;
  7.  
  8. //fill the object and calculate totalTime
  9. input.forEach((line) => {
  10. let [command, band, other] = line.split('; ');
  11.  
  12. if (command === 'Play') {
  13. let time = +other;
  14. if (!concert.hasOwnProperty(band)) {
  15. concert[band] = {};
  16. concert[band]['time'] = 0;
  17. concert[band]['members'] = [];
  18. }
  19. let currentTime = +concert[band]['time'];
  20. concert[band]['time'] = currentTime + time;
  21. totalTime += time;
  22. } else {
  23. let members = other.split(', ');
  24. if (!concert.hasOwnProperty(band)) {
  25. concert[band] = {};
  26. concert[band]['time'] = 0;
  27. concert[band]['members'] = [];
  28. }
  29. let oldMembers = concert[band]['members'];
  30. let newMembers = [...oldMembers,...members];
  31. let set = new Set(newMembers);
  32. concert[band]['members'] = Array.from(set);
  33.  
  34. }
  35. });
  36.  
  37.  
  38. console.log(`Total time: ${totalTime}`);
  39.  
  40.  
  41. let concertArr = Object.entries(concert);
  42. let sorted = concertArr.sort((a, b) => {
  43. return b[1]['time'] - a[1]['time'];
  44. });
  45.  
  46. sorted.forEach((line) => {
  47. console.log(`${line[0]} -> ${line[1]['time']}`);
  48. });
  49.  
  50. console.log(finalBand);
  51.  
  52. let final = concert[finalBand]['members'];
  53. final.forEach((line) => {
  54. console.log(`=> ${line}`);
  55. })
  56. }
  57.  
  58. solve(['Play; The Beatles; 2584',
  59. 'Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr',
  60. 'Add; Eagles; Glenn Frey, Don Henley, Bernie Leadon, Randy Meisner',
  61. 'Play; Eagles; 1869',
  62. 'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards',
  63. 'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards, Bill Wyman, Charlie Watts, Ian Stewart',
  64. 'Play; The Rolling Stones; 4239',
  65. 'start of concert',
  66. 'The Rolling Stones']);
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement