Advertisement
Todorov_Stanimir

04. Roli The Coder With Set

Jul 29th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. roliTheCoder = (input) => {
  2.     let pattern = /(?<id>[\d]+)([ ]+)#(?<name>[^@]+)([ ]*)(?<participants>[@A-Za-z0-9'\- ]+)*/g;
  3.     let events = {};
  4.     for (let line of input) {
  5.         if (line === 'Time for Code') {
  6.             break;
  7.         }
  8.         if (line.match(pattern)) {
  9.             let result = pattern.exec(line)
  10.             let id = Number(result.groups.id);
  11.             let name = result.groups.name.trim();
  12.             let participants = [];
  13.             if (result.groups.participants) {
  14.                 participants = [...new Set(result.groups.participants.split(' ').filter(el => el !== ''))];
  15.             }
  16.             if (!events[id]) {
  17.                 events[id] = { name, length: participants.length, participants }
  18.             } else if (events[id].name === name) {
  19.                 let currentParticipants = events[id].participants;
  20.                 if (result.groups.participants) {
  21.                     participants = [...new Set(result.groups.participants.split(' ').filter(el => el !== '').concat(currentParticipants))];
  22.                     events[id] = { name, length: participants.length, participants }
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     Object.entries(events)
  28.         .sort((a, b) => b[1].length - a[1].length || a[1].name.localeCompare(b[1].name))
  29.         .forEach(event => {
  30.             console.log(`${event[1].name} - ${event[1].length}`)
  31.             event[1].participants.sort((a, b) => a.localeCompare(b)).forEach(participant => console.log(`${participant}`));
  32.         });
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement