Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let players = input.shift().split(', ');
- let line = input.shift();
- let namePattern = /(?<name>[A-Za-z]+)/g;
- let scorePattern = /(?<digit>\d)/g;
- let classation = {};
- while (line != 'end of race') {
- let currName = '';
- let currScore = 0;
- while ((currChar = namePattern.exec(line)) != null) {
- currName += currChar.groups.name;
- }
- while ((currDigit = scorePattern.exec(line)) != null) {
- currScore += Number(currDigit.groups.digit);
- }
- if (players.includes(currName)) {
- if (!classation.hasOwnProperty(currName)) {
- classation[currName] = currScore;
- } else {
- classation[currName] += currScore;
- }
- }
- line = input.shift();
- }
- let winners = Object.entries(classation).sort((a, b) => b[1] - a[1]);
- for (let i = 0; i < 3; i++) {
- if (i == 0) {
- console.log(`${i + 1}st place: ${winners[i][0]}`);
- } else if (i == 1) {
- console.log(`${i + 1}nd place: ${winners[i][0]}`);
- } else {
- console.log(`${i + 1}rd place: ${winners[i][0]}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement