Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve3(input=[]) {
- input.pop();
- let heroNumber = input.shift();
- let heros = {};
- for (let i = 0; i < heroNumber; i++) {
- let [hero, hitP, manaP] = input.shift().split(' ');
- hitP = Number(hitP);
- manaP = Number(manaP);
- if (hitP > 100) {
- hitP = 100;
- }
- if (manaP > 200) {
- manaP = 200;
- }
- heros[hero] = {'hitP':hitP, 'manaP':manaP};
- }
- for (const line of input) {
- let [command, hero, amount, name] = line.split(' - ');
- amount = Number(amount);
- switch (command) {
- case 'CastSpell':
- if (heros[hero].manaP >= amount) {
- heros[hero].manaP -= amount;
- console.log(`${hero} has successfully cast ${name} and now has ${heros[hero].manaP} MP!`);
- } else {
- console.log(`${hero} does not have enough MP to cast ${name}!`);
- }
- break;
- case 'TakeDamage':
- if (heros[hero].hitP > amount) {
- heros[hero].hitP -= amount;
- console.log(`${hero} was hit for ${amount} HP by ${name} and now has ${heros[hero].hitP} HP left!`);
- } else {
- console.log(`${hero} has been killed by ${name}!`);
- delete heros[hero];
- }
- break;
- case 'Recharge':
- if (heros[hero].manaP + amount > 200) {
- amount = 200 - heros[hero].manaP;
- heros[hero].manaP = 200;
- console.log(`${hero} recharged for ${amount} MP!`);
- } else {
- heros[hero].manaP += amount;
- console.log(`${hero} recharged for ${amount} MP!`);
- }
- break;
- case 'Heal':
- if (heros[hero].hitP + amount > 100) {
- amount = 100 - heros[hero].hitP;
- heros[hero].hitP = 100;
- console.log(`${hero} healed for ${amount} HP!`);
- } else {
- heros[hero].hitP += amount;
- console.log(`${hero} healed for ${amount} HP!`);
- }
- break;
- }
- }
- let herosArray = Object.entries(heros);
- herosArray.sort(sortHeroes);
- function sortHeroes(a, b) {
- let nameA = a[0];
- let hitA = a[1].hitP;
- let nameB = b[0];
- let hitB = b[1].hitP;
- if (hitA === hitB) {
- return nameA.localeCompare(nameB);
- } else {
- return hitB - hitA;
- }
- }
- let sortedHeros = Object.fromEntries(herosArray);
- for (const hero in sortedHeros) {
- console.log(hero);
- console.log(` HP: ${sortedHeros[hero].hitP}`);
- console.log(` MP: ${sortedHeros[hero].manaP}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement