Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let gladiators = {};
- for (let info of arr) {
- if (!info.includes('vs') && info !== 'Ave Cesar') {
- let tokens = info.split(' -> ');
- const gladiator = tokens[0];
- const skill = tokens[1];
- const skillPower = Number(tokens[2]);
- if (!(gladiator in gladiators)) { gladiators[gladiator] = {}; }
- if (!(skill in gladiators[gladiator])) { gladiators[gladiator][skill] = skillPower }
- if (gladiators[gladiator][skill] < skillPower) { gladiators[gladiator][skill] = skillPower }
- } else if (info.includes('vs')) {
- const [gladiator1, gladiator2] = info.split(' vs ');
- const doesExist1 = Object.keys(gladiators).includes(gladiator1);
- const doesExist2 = Object.keys(gladiators).includes(gladiator2);
- if (doesExist1 && doesExist2) {
- for (const skill of Object.keys(gladiators[gladiator1])) {
- if (skill in gladiators[gladiator2]) {
- const firstGL = Object.values(gladiators[gladiator1]).reduce((a, b) => a + b);
- const secondGL = Object.values(gladiators[gladiator2]).reduce((a, b) => a + b);
- (firstGL > secondGL) ? delete gladiators[gladiator2] : delete gladiators[gladiator1]
- }
- }
- }
- }
- }
- gladiators = [...Object.entries(gladiators)]
- .sort((a, b) => Object.values(b[1]).reduce((a, b) => a + b) - Object.values(a[1]).reduce((a, b) => a + b) || a[0].localeCompare(b[0]))
- for (const gladiator of gladiators) {
- console.log(`${gladiator[0]}: ${Object.values(gladiator[1]).reduce((a, b) => a + b)} skill`);
- const skills = [...Object.entries(gladiator[1])].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
- skills.forEach(x => console.log(`- ${x[0]} <!> ${x[1]}`));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement