Advertisement
kStoikow

qw

Oct 19th, 2019
924
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 = /([+|-]?[0-9]+[.]*?[0-9]+|[+|-]?[0-9]+)/g;
  7.     let dmgPattern = /(\*|\/)/g;
  8.  
  9.     for (const demon of demonsArr) {
  10.         let hp = 0;
  11.         let dmg = 0;
  12.         if (demon.match(healthPattern) != null) {
  13.             for (const char of demon.match(healthPattern)) {
  14.                 hp += char.charCodeAt(0);
  15.             }
  16.         }
  17.  
  18.         let digits = demon.match(sumPattern);
  19.        
  20.         if (digits != null) {
  21.             for (const digit of digits) {
  22.                 dmg += Number(digit);
  23.             }
  24.         }
  25.  
  26.         let subtractOrMultiply = demon.match(dmgPattern);
  27.  
  28.         if (subtractOrMultiply != null) {
  29.             for (const operand of subtractOrMultiply) {
  30.                 if (operand == '*') {
  31.                     dmg *= 2;
  32.                 } else {
  33.                     dmg /= 2;
  34.                 }
  35.             }
  36.         }
  37.  
  38.         demons[demon] = {};
  39.         demons[demon]['health'] = hp;
  40.         demons[demon]['damage'] = dmg;
  41.     }
  42.  
  43.     let sorted = Object.entries(demons).sort((a, b) => a[0].localeCompare(b[0]));
  44.     for (const [name, valuesObj] of sorted) {
  45.         console.log(`${name} - ${valuesObj['health']} health, ${valuesObj['damage'].toFixed(2)} damage`);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement