Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let playerPool = {};
- for (let line of input) {
- if (line === "Season end") {
- break;
- }
- if (line.includes(' -> ')) {
- let [player, position, skill] = line.split(' -> ');
- if (!playerPool[player]) {
- let positionAndSkill = {};
- playerPool[player] = positionAndSkill;
- positionAndSkill[position] = Number(skill);
- } else {
- if (!playerPool[player][position]) {
- playerPool[player][position] = Number(skill);
- } else {
- let currentSkill = playerPool[player][position];
- if (currentSkill < Number(skill)) {
- playerPool[player][position] = Number(skill);
- }
- }
- }
- } else if (line.includes(' vs ')) {
- let [firstPlayer, secondPlayer] = line.split(' vs ');
- if (playerPool.hasOwnProperty(firstPlayer) && playerPool.hasOwnProperty(secondPlayer)) {
- let firstPositions = Object.keys(playerPool[firstPlayer]);
- let secondPositions = Object.keys(playerPool[secondPlayer]);
- let firstTotalSkills = Object.values(playerPool[firstPlayer]).reduce((a, b) => a + b, 0);
- let secondTotalSkills = Object.values(playerPool[secondPlayer]).reduce((a, b) => a + b, 0);
- const found = firstPositions.some(element => secondPositions.indexOf(element) >= 0);
- if (found) {
- firstTotalSkills > secondTotalSkills
- ? delete playerPool[secondPlayer]
- : delete playerPool[firstPlayer];
- } else {
- continue;
- }
- }
- }
- }
- let sorted = Object.entries(playerPool)
- .sort((first, second) => Object.values(second[1]).reduce((a, b) => a + b, 0) - Object.values(first[1]).reduce((a, b) => a + b, 0) || first[0].localeCompare(second[0]))
- .forEach(element => {
- let totalSkill = Object.values(element[1]).reduce((a, b) => a + b, 0);
- console.log(`${element[0]}: ${totalSkill} skill`);
- let sortedData = Object.entries(element[1]);
- sortedData.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
- .forEach(item => {
- console.log(`- ${item[0]} <::> ${item[1]}`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement