Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.    
  3.    let initial = input
  4.    .shift()
  5.    .split(', ');
  6.    
  7.    let partisipants = {};
  8.    initial.forEach(curr => {
  9.        partisipants[curr] = 0;
  10.    });
  11. let namePattern = /[a - zA - Z]/g;
  12. let distPattern = /\d/g;
  13.  
  14.  let racers = input.slice(0, input.indexOf('end of race'))
  15.  .forEach(line => {
  16.      let name = line.match(namePattern).join('');
  17.      let distance = line.match(distPattern).map(Number).reduce((a, c) => a + c);
  18.      if (partisipants.hasOwnProperty(name)){
  19.         partisipants[name] += distance;
  20.      }    
  21.  })
  22.  let output = Object.entries(partisipants).sort((a,b) => b[1] - a[1]);
  23. console.log(`1st place: ${output[0][0]}
  24.     2nd place: ${output[1][0]}
  25.     3rd place: ${output[2][0]}`);
  26. };
  27.  
  28. solve('George, Peter, Bill, Tom',
  29.     'G4e@55or%6g6!68e!!@',
  30.     'R1@!3a$y4456@',
  31.     'B5@i@#123ll',
  32.     'G@e54o$r6ge#',
  33.     '7P%et^#e5346r',
  34.     'T$o553m&6',
  35.     'end of race'
  36.        );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement