Advertisement
vladovip

Concert JS FUND FINEXV2

Aug 18th, 2022
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let concert = {};
  3.     for (let line of input) {
  4.         let [command, band, params] = line.split('; ');
  5.  
  6.         if (command == 'Start!') {
  7.             break;
  8.         }
  9.         if (command === 'Play') {
  10.             if (!concert.hasOwnProperty(band)) {
  11.                 concert[band] = {
  12.                     time: 0,
  13.                     members: []
  14.                 }
  15.                 concert[band].time += Number(params)
  16.             } else {
  17.                 concert[band].time += Number(params)
  18.             }
  19.         } else {
  20.             let membersToAdd = params.split(', ')
  21.             if (!concert.hasOwnProperty(band)) {
  22.                 concert[band] = {
  23.                     time: 0,
  24.                     members: []
  25.                 }
  26.                 for (const member of membersToAdd) {
  27.                     concert[band].members.push(member)
  28.                 }
  29.             } else {
  30.                 for (const member of membersToAdd) {
  31.                     if (!concert[band].members.includes(member)) {
  32.                         concert[band].members.push(member)
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     let totalSum = 0
  39.     for (let band in concert) {
  40.         totalSum += concert[band].time
  41.     }
  42.     console.log(`Total time: ${totalSum}`);
  43.  
  44.     for (let band in concert) {
  45.         console.log(`${band} -> ${concert[band].time}`);
  46.     }
  47.  
  48.     let firstBand = Object.entries(concert)[0]
  49.  
  50.     console.log(firstBand[0]);
  51.     for (let name of firstBand[1].members) {
  52.         console.log(`=> ${name}`);
  53.     }
  54. }
  55.  
  56.  
  57. solve([
  58.     "Play; The Beatles; 2584",
  59.     "Add; The Beatles; John Lennon, George Harrison, Ringo Starr",
  60.     "Add; The Beatles; Paul McCartney, George Harrison",
  61.     "Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards",
  62.     "Play; The Rolling Stones; 4239",
  63.     "Start!",
  64.   ]);
  65.  
  66.   console.log(`------------`);
  67.  
  68.   solve([
  69.     "Add; The Beatles; John Lennon, Paul McCartney",
  70.     "Add; The Beatles; Paul McCartney, George Harrison",
  71.     "Add; The Beatles; George Harrison, Ringo Starr",
  72.     "Play; The Beatles; 3698",
  73.     "Play; The Beatles; 3828",
  74.     "Start!",
  75.   ]);
  76.  
  77.   console.log(`------------`);
  78.  
  79.   solve([
  80.     "Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr",
  81.     "Play; The Beatles; 4569",
  82.     "Play; The Beatles; 2456",
  83.     "Play; Queen; 1250",
  84.     "Add; Queen; Freddie Mercury, Brian May, Roger Taylor, John Deacon",
  85.     "Play; Queen; 6215",
  86.     "Start!",
  87.   ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement