maymunskoa

02. Rage Quit (RegEx)

Mar 21st, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let line = input.slice(0).shift();
  3.  
  4.   let pattern = /(?<symbols>[^\d]+)(?<number>\d+)/gm;
  5.   let symbolSet = new Set();
  6.   let matches = Array.from(line.matchAll(pattern));
  7.   let output = [];
  8.  
  9.   if (matches.length > 0) {
  10.     for (const match of matches) {
  11.         if(Number(match[2]) !== 0){
  12.             let currentMatch = match[1].toUpperCase().repeat(Number(match[2]));
  13.             output.push(currentMatch);
  14.             match[1].split("").forEach(symbol => {
  15.                 symbolSet.add(symbol.toUpperCase());
  16.               });
  17.             }
  18.         }
  19.   }
  20.   console.log(`Unique symbols used: ${Array.from(symbolSet).length}`);
  21.   console.log(output.join(""));
  22. }
Advertisement
Add Comment
Please, Sign In to add comment