Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve3(input=[]) {
  2.     input.pop();
  3.  
  4.     let heroNumber = input.shift();
  5.     let heros = {};
  6.  
  7.     for (let i = 0; i < heroNumber; i++) {
  8.         let [hero, hitP, manaP] = input.shift().split(' ');
  9.         hitP = Number(hitP);
  10.         manaP = Number(manaP);
  11.  
  12.         if (hitP > 100) {
  13.             hitP = 100;
  14.         }
  15.         if (manaP > 200) {
  16.             manaP = 200;
  17.         }
  18.  
  19.         heros[hero] = {'hitP':hitP, 'manaP':manaP};
  20.     }
  21.  
  22.     for (const line of input) {
  23.         let [command, hero, amount, name] = line.split(' - ');
  24.         amount = Number(amount);
  25.  
  26.         switch (command) {
  27.  
  28.             case 'CastSpell':
  29.                 if (heros[hero].manaP >= amount) {
  30.                     heros[hero].manaP -= amount;
  31.                     console.log(`${hero} has successfully cast ${name} and now has ${heros[hero].manaP} MP!`);
  32.                 } else {
  33.                     console.log(`${hero} does not have enough MP to cast ${name}!`);
  34.                 }
  35.                 break;
  36.  
  37.             case 'TakeDamage':
  38.                 if (heros[hero].hitP > amount) {
  39.                     heros[hero].hitP -= amount;
  40.                     console.log(`${hero} was hit for ${amount} HP by ${name} and now has ${heros[hero].hitP} HP left!`);
  41.                 } else {
  42.                     console.log(`${hero} has been killed by ${name}!`);
  43.                     delete heros[hero];
  44.                 }
  45.                 break;
  46.  
  47.             case 'Recharge':
  48.                 if (heros[hero].manaP + amount > 200) {
  49.                     amount = 200 - heros[hero].manaP;
  50.                     heros[hero].manaP = 200;
  51.                     console.log(`${hero} recharged for ${amount} MP!`);
  52.                 } else {
  53.                     heros[hero].manaP += amount;
  54.                     console.log(`${hero} recharged for ${amount} MP!`);
  55.                 }
  56.                 break;
  57.  
  58.             case 'Heal':
  59.                 if (heros[hero].hitP + amount > 100) {
  60.                     amount = 100 - heros[hero].hitP;
  61.                     heros[hero].hitP = 100;
  62.                     console.log(`${hero} healed for ${amount} HP!`);
  63.                 } else {
  64.                     heros[hero].hitP += amount;
  65.                     console.log(`${hero} healed for ${amount} HP!`);
  66.                 }
  67.                 break;
  68.         }
  69.     }
  70.  
  71.     let herosArray = Object.entries(heros);
  72.  
  73.     herosArray.sort(sortHeroes);
  74.  
  75.     function sortHeroes(a, b) {
  76.         let nameA = a[0];
  77.         let hitA = a[1].hitP;
  78.  
  79.         let nameB = b[0];
  80.         let hitB = b[1].hitP;
  81.  
  82.         if (hitA === hitB) {
  83.             return nameA.localeCompare(nameB);
  84.         } else {
  85.             return hitB - hitA;
  86.         }
  87.     }
  88.  
  89.     let sortedHeros = Object.fromEntries(herosArray);
  90.  
  91.     for (const hero in sortedHeros) {
  92.         console.log(hero);
  93.         console.log(`  HP: ${sortedHeros[hero].hitP}`);
  94.         console.log(`  MP: ${sortedHeros[hero].manaP}`);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement