Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr) {
- // functions
- const calculateSkillPoints = entries => {
- let result = 0;
- entries.forEach(entrie => {
- let points = entrie[1];
- result += points;
- });
- return result;
- }
- let gladiators = new Map();
- let command = inputArr.shift();
- while(command !== 'Ave Cesar') {
- if(command.includes(' vs ')) {
- let [firstGladiator, secondGladiator] = command.split(' vs ');
- if(gladiators.has(firstGladiator) && gladiators.has(secondGladiator)) {
- let haveTechniqueInCommon = false;
- let firstGladiatorEntries = Object.entries(gladiators.get(firstGladiator));
- let secondGladiatorEntries = Object.entries(gladiators.get(secondGladiator));
- for(let i = 0; i < firstGladiatorEntries.length; i++) {
- let firstGSkill = firstGladiatorEntries[i][0];
- for(let j = 0; j < secondGladiatorEntries.length; j++) {
- let secondGSkill = secondGladiatorEntries[j][0];
- if(firstGSkill == secondGSkill) {
- haveTechniqueInCommon = true;
- break;
- }
- }
- }
- if(haveTechniqueInCommon) {
- let firstGladiatorTotalSkillPoints = calculateSkillPoints(firstGladiatorEntries);
- let secondGladiatorTotalSkillPoints = calculateSkillPoints(secondGladiatorEntries);
- if(firstGladiatorTotalSkillPoints > secondGladiatorTotalSkillPoints) {
- gladiators.delete(secondGladiator);
- } else {
- gladiators.delete(firstGladiator);
- }
- }
- }
- // 3... 2... 1... Round One:... FIGHT!
- } else {
- let [gladiatorName, technique, skillPoint] = command.split(' -> ');
- skillPoint = Number(skillPoint);
- if(!gladiators.has(gladiatorName)) {
- let obj = {
- [technique]: skillPoint,
- }
- gladiators.set(gladiatorName, obj);
- } else {
- if(gladiators.get(gladiatorName)[technique] !== undefined) {
- let oldSkillPoints = gladiators.get(gladiatorName)[technique];
- if(skillPoint > oldSkillPoints) {
- gladiators.get(gladiatorName)[technique] = skillPoint;
- }
- // ==============================================================
- } else {
- gladiators.get(gladiatorName)[technique] = skillPoint;
- }
- }
- }
- command = inputArr.shift();
- }
- let sortedEntries = Array.from(gladiators.entries()).sort((a, b) => {
- let totalSkillPointA = calculateSkillPoints(Object.entries(a[1]));
- let totalSkillPointB = calculateSkillPoints(Object.entries(b[1]));
- if(totalSkillPointA !== totalSkillPointB) {
- return totalSkillPointB - totalSkillPointA;
- } else {
- return a[0].localeCompare(b[0]);
- }
- });
- sortedEntries.forEach(entrie => {
- let [name, skills] = entrie;
- let skillEntries = Object.entries(skills);
- let totalSkill = calculateSkillPoints(skillEntries);
- console.log(`${name}: ${totalSkill} skill`);
- skillEntries.sort((a, b) => {
- let techniquePointsA = a[1];
- let techniquePointsB = b[1];
- if(techniquePointsA != techniquePointsB) {
- return techniquePointsB - techniquePointsA;
- } else {
- return a[0].localeCompare(b[0]);
- }
- });
- skillEntries.forEach(entrie => {
- console.log(`- ${entrie[0]} <!> ${entrie[1]}`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement