Advertisement
kStoikow

Untitled

Oct 15th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let players = input.shift().split(', ');
  3.     let line = input.shift();
  4.     let namePattern = /(?<name>[A-Za-z]+)/g;
  5.     let scorePattern = /(?<digit>\d)/g;
  6.     let classation = {};
  7.  
  8.     while (line != 'end of race') {
  9.         let currName = '';
  10.         let currScore = 0;
  11.         while ((currChar = namePattern.exec(line)) != null) {
  12.             currName += currChar.groups.name;
  13.         }
  14.  
  15.         while ((currDigit = scorePattern.exec(line)) != null) {
  16.             currScore += Number(currDigit.groups.digit);
  17.         }
  18.  
  19.         if (players.includes(currName)) {
  20.             if (!classation.hasOwnProperty(currName)) {
  21.                 classation[currName] = currScore;
  22.             } else {
  23.                 classation[currName] += currScore;
  24.             }
  25.         }
  26.  
  27.         line = input.shift();
  28.     }
  29.  
  30.     let winners = Object.entries(classation).sort((a, b) => b[1] - a[1]);
  31.  
  32.     for (let i = 0; i < 3; i++) {
  33.         if (i == 0) {
  34.             console.log(`${i + 1}st place: ${winners[i][0]}`);
  35.         } else if (i == 1) {
  36.             console.log(`${i + 1}nd place: ${winners[i][0]}`);
  37.         } else {
  38.             console.log(`${i + 1}rd place: ${winners[i][0]}`);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement