simeonshopov

Arena Tier

Apr 14th, 2021
232
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   let gladiators = {};
  3.  
  4.   for (let info of arr) {
  5.     if (!info.includes('vs') && info !== 'Ave Cesar') {
  6.       let tokens = info.split(' -> ');
  7.       const gladiator = tokens[0];
  8.       const skill = tokens[1];
  9.       const skillPower = Number(tokens[2]);
  10.       if (!(gladiator in gladiators)) { gladiators[gladiator] = {}; }
  11.       if (!(skill in gladiators[gladiator])) { gladiators[gladiator][skill] = skillPower }
  12.       if (gladiators[gladiator][skill] < skillPower) { gladiators[gladiator][skill] = skillPower }
  13.     } else if (info.includes('vs')) {
  14.       const [gladiator1, gladiator2] = info.split(' vs ');
  15.       const doesExist1 = Object.keys(gladiators).includes(gladiator1);
  16.       const doesExist2 = Object.keys(gladiators).includes(gladiator2);
  17.       if (doesExist1 && doesExist2) {
  18.         for (const skill of Object.keys(gladiators[gladiator1])) {
  19.           if (skill in gladiators[gladiator2]) {
  20.             const firstGL = Object.values(gladiators[gladiator1]).reduce((a, b) => a + b);
  21.             const secondGL = Object.values(gladiators[gladiator2]).reduce((a, b) => a + b);
  22.             (firstGL > secondGL) ? delete gladiators[gladiator2] : delete gladiators[gladiator1]
  23.           }
  24.         }
  25.       }
  26.     }
  27.   }
  28.  
  29.   gladiators = [...Object.entries(gladiators)]
  30.   .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]))
  31.  
  32.   for (const gladiator of gladiators) {
  33.     console.log(`${gladiator[0]}: ${Object.values(gladiator[1]).reduce((a, b) => a + b)} skill`);
  34.     const skills = [...Object.entries(gladiator[1])].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
  35.     skills.forEach(x => console.log(`- ${x[0]} <!> ${x[1]}`));
  36.   }
  37. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×