kStoikow

Untitled

Oct 17th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let splitPattern = /, */g;
  3.     let demonsArr = input[0].split(splitPattern);
  4.     let demons = {};
  5.     let healthPattern = /([^0-9^\.^\/^\+^\-^\*])/g;
  6.     let sumPattern = /([+]?\d?[.]?\d+|[-]?\d?[.]?\d+)/g;
  7.     let dmgPattern = /(\*|\/)/g;
  8.  
  9.     for (const demon of demonsArr) {
  10.         let hp = 0;
  11.         let dmg = 0;
  12.         for (const currHp of demon.match(healthPattern)) {
  13.             hp += currHp.charCodeAt(0);
  14.         }
  15.  
  16.         let digits = demon.match(sumPattern);
  17.         if (digits != null) {
  18.             for (const digit of digits) {
  19.                 dmg += Number(digit);
  20.             }
  21.         }
  22.  
  23.         let subtractOrMultiply = demon.match(dmgPattern);
  24.         if (subtractOrMultiply != null) {
  25.             for (const operand of subtractOrMultiply) {
  26.                 if (operand == '*') {
  27.                     dmg = dmg * 2;
  28.                 } else {
  29.                     dmg = dmg / 2;
  30.                 }
  31.             }
  32.         }
  33.  
  34.         demons[demon] = {};
  35.         demons[demon]['health'] = hp;
  36.         demons[demon]['damage'] = dmg;
  37.     }
  38.  
  39.     let sorted = Object.entries(demons).sort((a, b) => a[0].localeCompare(b[0]));
  40.     for (const [name, valuesObj] of sorted) {
  41.         console.log(`${name} - ${valuesObj['health']} health, ${valuesObj['damage'].toFixed(2)} damage`);
  42.     }
  43. }
  44. solve(['M3ph*1.55st0**,  qweqe%$124'])
Advertisement
Add Comment
Please, Sign In to add comment