Advertisement
KrasimirKosturkov

Untitled

Apr 3rd, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let band = {};
  3.     let bandCount = {};
  4.     let totalTime = 0;
  5.     let group = input.pop();
  6.     let arr = [];
  7.     let set = new Set();
  8.     for(let line of input){
  9.         if(line === 'start of concert'){
  10.             break;
  11.         }
  12.         let [addOrPlay, bandName, timeOrSingers] = line.split('; ');
  13.         if(addOrPlay === 'Add'){
  14.            arr = timeOrSingers.split(', ');
  15.            for(let i = 0; i < arr.length; i++){
  16.             if(!band.hasOwnProperty(bandName)){
  17.                 band[bandName] = [arr[i]];
  18.             }else if(band.hasOwnProperty(bandName)){
  19.                 band[bandName].push(arr[i]);
  20.             }
  21.        }
  22.         }else if(addOrPlay === 'Play'){
  23.             if(!bandCount.hasOwnProperty(bandName)){
  24.                 bandCount[bandName] = +(timeOrSingers);
  25.                 totalTime += +(timeOrSingers);
  26.             }else{
  27.                 bandCount[bandName] += +(timeOrSingers);
  28.                totalTime += +(timeOrSingers);
  29.             }
  30.         }
  31.     }
  32.     let arrayBandCount = Object.entries(bandCount);
  33.     arrayBandCount.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
  34.     console.log(`Total time: ${totalTime}`);
  35.     for(let item of arrayBandCount){
  36.         console.log(`${item[0]} -> ${item[1]}`);
  37.     }
  38.    
  39.     let result = Object.entries(band);
  40.     console.log(group);
  41.     for(let j = 0; j < result.length; j++){
  42.  
  43.     for(let i = 0; i < result[j][1].length; i++){
  44.         if(result[j][1][i] === result[0][1][i+1]){
  45.             let index = result[j][1].indexOf(result[j][1][i+1]);
  46.             result[j][1].splice(index, 1);
  47.         }
  48.     }
  49.     if(result[j][0] === group){
  50.         for(let boy of result[j][1]){
  51.             set.add(boy);
  52.         }
  53.     }
  54.    }
  55.    let arrResult = Array.from(set);
  56.    console.log(`=> ${arrResult.join('\n=> ')}`);
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement