Advertisement
maymunskoa

05. Nether Realms (RegExp)

Mar 19th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(entry) {
  2.   let data = entry
  3.               .shift()
  4.               .split(/[,\s]+/g)
  5.               .sort((a,b)=>a.localeCompare(b));
  6.  
  7.   let healEx = /[^\d+\-*/\.]/g;
  8.   let numEx = /([+-]?[0-9]+\.?[0-9]*)/g;
  9.   let signEx = /[\/\*]/g;
  10.  
  11.  
  12.   for (let line of data) {
  13.     line = line.trim();
  14.     let health = 0;
  15.     let base = 0;
  16.     while((match = healEx.exec(line)) !== null){
  17.        health += match[0].charCodeAt();
  18.     }
  19.  
  20.  
  21.     while((match = numEx.exec(line)) !== null){
  22.           base+=Number(match[0]);
  23.         }
  24.       while((match = signEx.exec(line)) !== null){
  25.         if(match[0] === '/'){
  26.           base/=2;
  27.         }else{
  28.           base*=2;
  29.         }
  30.       }
  31.    console.log(`${line} - ${health} health, ${base.toFixed(2)} damage`);
  32.   }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement