Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let listOfPart = input.shift().split(', ');
- // console.log(listOfPart);
- let arr = [];
- let arr2 = [];
- let i = 0;
- let obj = {};
- while (input[i] !== 'end of race') {
- let cur = input[i].split('');
- cur.forEach(el => /[A-Za-z]|[0-9]/.test(el) ? arr.push(el) : false);
- arr2.push(arr.join(''));
- arr = [];
- i++;
- }
- for (let el of arr2) {
- let name = el.split('').filter(el2 => /[A-Za-z]/.test(el2));
- let distance = 0;
- el.split('').forEach(el3 => /[0-9]/.test(el3) ? distance += Number(el3) : false);
- if (obj.hasOwnProperty(name.join(''))) {
- obj[name.join('')].push(distance);
- } else if (listOfPart.includes(name.join(''))) {
- obj[name.join('')] = [distance];
- }
- }
- for (let key in obj) {
- obj[key] = obj[key].reduce((acc, el) => acc + el, 0);
- }
- let arr3 = Object.entries(obj).sort((a, b) => b[1] - a[1]);
- // console.log(obj);
- console.log(`1st place: ${arr3[0][0]}
- 2nd place: ${arr3[1][0]}
- 3rd place: ${arr3[2][0]}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment