ErolKZ

Untitled

Nov 23rd, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4. let pattern = /[^0-9\+\-\*\/\.]/;
  5.  
  6. let pattern2 = /[0-9\+\-\*\/\.]/;
  7.  
  8. let workNames = input.split(/[\,][\s*]/);
  9.  
  10. let arr = [];
  11.  
  12.  
  13. workNames = workNames.map(el => el.split(''));
  14.  
  15. for (let el of workNames) {
  16.  
  17. let filteredName = el.filter(str => str.match(pattern));
  18.  
  19. let arithSymbols = el.filter(str => str.match(pattern2));
  20.  
  21. let sum = arithSymbols.filter(el2 => el2.match(/^[+-]?\d+(\.\d+)?$/)).reduce((acc, el3) => acc + Number(el3), 0);
  22.  
  23. sum = arithSymbols.filter(el2 => el2.match(/[*\/]/)).reduce((acc, el3) => el3.match(/[*]/) ? acc * 2 : acc / 2, sum);
  24.  
  25. let totalHealth = filteredName.reduce((acc, el2) => acc + el2.charCodeAt(), 0);
  26.  
  27. arr.push([el.join(''), { health: totalHealth, damage: sum.toFixed(2) }]);
  28.  
  29.  
  30. }
  31.  
  32.  
  33. arr.sort((a, b) => a[0].localeCompare(b[0]));
  34.  
  35. // console.log(arr);
  36.  
  37. for (let el of arr) {
  38.  
  39. console.log(`${el[0]} - ${el[1]['health']} health, ${el[1]['damage']} damage`);
  40.  
  41. }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. solve(
  48.  
  49. // 'M3ph1st0**, Azazel'
  50.  
  51. 'M3ph-0.5s-0.5t0.0**'
  52.  
  53. );
Add Comment
Please, Sign In to add comment