Advertisement
dimoBs

09. Arena Tier

Mar 5th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) { let list = {}
  2.     for (let el of arr) {
  3.       if (el === 'Ave Cesar') {
  4.         break
  5.       } else if (el.includes(' -> ')) {
  6.         add(el)
  7.       } else if (el.includes(' vs ')) {
  8.         battle(el)
  9.       }    }
  10.     let tier = Object.entries(list)
  11.     let array = []
  12.     for (let elem of tier) {
  13.       let name = elem[0]
  14.       let pow = Object.entries(elem[1])
  15.       let sum = pow.map(a => a[1]).reduce((a, b) => a + b)
  16.       array.push([name, pow, sum])
  17.     }
  18.     array.sort((a, b) => b[2] - a[2] || a[0].localeCompare(b[0]))
  19.     for (let part of array) {
  20.       console.log(`${part[0]}: ${part[2]} skill`);
  21.       part[1]
  22.         .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
  23.         .map(x => console.log(`- ${x[0]} <!> ${x[1]}`))
  24.     }
  25.     function add(el) {
  26.       let [gladiator, skill, power] = el.split(' -> ')
  27.       power = Number(power)
  28.       if (!list.hasOwnProperty(gladiator)) {
  29.         list[gladiator] = {}
  30.         list[gladiator][skill] = power
  31.       }
  32.       else { if (!list[gladiator].hasOwnProperty(skill)) {
  33.           list[gladiator][skill] = power
  34.         }
  35.         else { let oldPow = list[gladiator][skill]
  36.           if (power > oldPow) {
  37.             list[gladiator][skill] = power
  38.           }        }      }    }
  39.     function battle(el) {
  40.       let [gladiatorA, gladiatorB] = el.split(' vs ')
  41.       if (list.hasOwnProperty(gladiatorA) && list.hasOwnProperty(gladiatorB)) {
  42.         let skillA = list[gladiatorA]
  43.         let skillB = list[gladiatorB]
  44.         for (let elA in skillA) {
  45.           for (let elB in skillB) {
  46.             if (elA === elB) {
  47.               if (skillA[elA] > skillB[elB]) {
  48.                 delete list[gladiatorB]
  49.               } else if (skillA[elA] < skillB[elB]) {
  50.                 delete list[gladiatorA]
  51.               }  }  }  }  }  }  }
  52.               solve(['Peter -> BattleCry -> 400','Alex -> PowerPunch -> 300','Stefan -> Duck -> 200','Stefan -> Tiger -> 250','Ave Cesar']);
  53. solve(['Pesho -> Duck -> 400','Julius -> Shield -> 150','Gladius -> Heal -> 200','Gladius -> Support -> 250','Gladius -> Shield -> 250',
  54. 'Peter vs Gladius','Gladius vs Julius','Gladius vs Maximilian', 'Ave Cesar']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement