Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
159
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.    
  17.      let name = line.match(namePattern).join('');
  18.      let distance = line.match(distPattern).map(Number).reduce((a, c) => a + c);
  19.      if (partisipants.hasOwnProperty(name)){
  20.         partisipants[name] += distance;
  21.      }    
  22.  })
  23.  let output = Object.entries(partisipants).sort((a,b) => b[1] - a[1]);
  24. console.log(`1st place: ${output[0][0]}
  25.     2nd place: ${output[1][0]}
  26.     3rd place: ${output[2][0]}`);
  27. };
  28.  
  29.  
  30. solve(['George, Peter, Bill, Tom',
  31.     'G4e@55or%6g6!68e!!@',
  32.     'R1@!3a$y4456@',
  33.     'B5@i@#123ll',
  34.     'G@e54o$r6ge#',
  35.     '7P%et^#e5346r',
  36.     'T$o553m&6',
  37.     'end of race']
  38.        );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement