Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let heroes = {};
- arr.forEach(line => {
- let [heroName, skill, points] = line.split(' -> ');
- points = Number(points);
- if (heroName != 'Ave Cesar') {
- if (points) {
- if (!heroes.hasOwnProperty(heroName)) {
- heroes[heroName] = [{ skill, points }];
- } else {
- for (let update in heroes[heroName]) {
- if (skill === heroes[heroName][update].skill) {
- if (points > heroes[heroName][update].points) {
- heroes[heroName][update].points = points;
- }
- } else {
- heroes[heroName].push({ skill, points });
- break;
- }
- }
- }
- } else {
- let [firstHero, secondHero] = heroName.split(' vs ')
- if (heroes.hasOwnProperty(firstHero) && heroes.hasOwnProperty(secondHero)) {
- Object.values(heroes[firstHero]).forEach(el1 => {
- Object.values(heroes[secondHero]).forEach(el2 => {
- if (el1.skill === el2.skill) {
- let totalFirstHero = Object.values(heroes[firstHero]).reduce((a, b) => a + b.points, 0);
- let totalSecondHero = Object.values(heroes[secondHero]).reduce((a, b) => a + b.points, 0);
- if (totalFirstHero > totalSecondHero) {
- delete heroes[secondHero];
- } else {
- delete heroes[firstHero];
- }
- }
- });
- });
- }
- }
- }
- });
- Object.entries(heroes).sort((a, b) => b[1].reduce((a, b) => a + b.points, 0) - a[1].reduce((a, b) => a + b.points, 0))
- .map(x => console.log(`${x[0]}: ${x[1].reduce((a, b) => a + b.points, 0)} skill\n${x[1]
- .sort((a,b)=>a.skill.localeCompare(b.skill)).sort((a,b)=>b.points-a.points)
- .map(x=>`- ${x.skill} <!> ${x.points}`).join('\n')}`));
- }
Advertisement
Add Comment
Please, Sign In to add comment