Advertisement
didkoslawow

Untitled

Mar 5th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   const gladiatorsInfo = [...gladiatorsInput];
  2.    const gladiators = {};
  3.    let index = 0;
  4.    let line = gladiatorsInfo[index];
  5.  
  6.    while (line !== 'Ave Cesar') {
  7.      if (line.includes('vs')) {
  8.        const [firsGladiator, secondGladiator] = line.split(' vs ');
  9.  
  10.        duel(gladiators, firsGladiator, secondGladiator);
  11.        index++;
  12.        line = gladiatorsInfo[index];
  13.        continue;
  14.      } else {
  15.        addGladiator(line);
  16.      }
  17.      index++;
  18.      line = gladiatorsInfo[index];
  19.    }
  20.  
  21.    sortAndPrint(gladiators);
  22.  
  23.    // logic for everything bellow:
  24.  
  25.    function addGladiator(line) {
  26.      [gladiatorName, technique, skill] = line.split(' -> ');
  27.      let totalSkill = 'totalSkill';
  28.      skill = Number(skill);
  29.  
  30.      if (!gladiators.hasOwnProperty(gladiatorName)) {
  31.        gladiators[gladiatorName] = {};
  32.        gladiators[gladiatorName][totalSkill] = 0;
  33.      }
  34.  
  35.      if (!gladiators[gladiatorName].hasOwnProperty(technique)) {
  36.        gladiators[gladiatorName][technique] = skill;
  37.        gladiators[gladiatorName][totalSkill] += skill;
  38.      } else {
  39.        if (skill > gladiators[gladiatorName][technique]) {
  40.          let difference = skill - gladiators[gladiatorName][technique];
  41.          gladiators[gladiatorName][technique] = skill;
  42.          gladiators[gladiatorName][totalSkill] += difference;
  43.        }
  44.      }
  45.    }
  46.  
  47.    function duel(gladiators, firstGladiator, secondGladiator) {
  48.      const firstExist = gladiators.hasOwnProperty(firstGladiator);
  49.      const secondExist = gladiators.hasOwnProperty(secondGladiator);
  50.  
  51.      const isValid = (firstG, secondG) => {
  52.        const first = Object.keys(gladiators[firstG]);
  53.        const second = Object.keys(gladiators[secondG]);
  54.  
  55.        return first.some(
  56.          (element) => element !== 'totalSkill' && second.includes(element)
  57.        );
  58.      };
  59.  
  60.      if (firstExist && secondExist && isValid(firstGladiator, secondGladiator)) {
  61.        const totalSkill = 'totalSkill';
  62.        if (
  63.          gladiators[firstGladiator][totalSkill] >
  64.          gladiators[secondGladiator][totalSkill]
  65.        ) {
  66.          delete gladiators[secondGladiator];
  67.        } else {
  68.          delete gladiators[firstGladiator];
  69.        }
  70.      }
  71.    }
  72.  
  73.    function sortAndPrint(gladiators) {
  74.      const sorted = Object.entries(gladiators).sort(
  75.        (a, b) => b[1].totalSkill - a[1].totalSkill || a[0].localeCompare(b[0])
  76.      );
  77.      for (const [name, skills] of sorted) {
  78.        const totalSkills = Object.entries(skills)[0][1];
  79.        delete skills.totalSkill;
  80.  
  81.        const sortedSkills = Object.entries(skills).sort(
  82.          (a, b) => b[1] - a[1] || a[0].localeCompare(b[0])
  83.        );
  84.  
  85.        console.log(`${name}: ${totalSkills} skill`);
  86.  
  87.        for (const [techniqueName, skill] of sortedSkills) {
  88.          console.log(`- ${techniqueName} <!> ${skill}`);
  89.        }
  90.      }
  91.    }
  92.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement