maymunskoa

3. Post Office (RegExp)

Mar 22nd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let string = input.shift();
  3.   let [first, second, third] = string.split("|");
  4.   let firstPattern = /([$\*#%&])([A-Z]+)\1/gm;
  5.   let secondPattern = /([0-9]{2}\:[0-9]{2})+/gm;
  6.   let firstGroup = Array.from(first.matchAll(firstPattern))[0][2];
  7.   let secondGroup = Array.from(second.match(secondPattern));
  8.   let ascis = [];
  9.   let lengths = [];
  10.   secondGroup.forEach(pair => {
  11.     let [asciCode, length] = pair.split(":");
  12.     ascis.push(Number(asciCode));
  13.     lengths.push(Number(length));
  14.   });
  15.  
  16.   let thirdGroup = third.split(" ");
  17.  
  18.   for (const letter of firstGroup) {
  19.     thirdGroup.forEach(word => {
  20.       let index = ascis.indexOf(letter.charCodeAt());
  21.       if (word[0] === letter && word.length === lengths[index] + 1) {
  22.         console.log(word);
  23.       }
  24.     });
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment