Advertisement
Guest User

03. Spy Master

a guest
Oct 19th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    
  3.     function decode(str){
  4.         return str
  5.             .replace(/!/g,'1')
  6.             .replace(/%/g,'2')
  7.             .replace(/#/g,'3')
  8.             .replace(/\$/g,'4')
  9.             .toLowerCase();
  10.     }
  11.  
  12.     function createMatcher(str){
  13.         let modifiedStr = str.split('')
  14.             .map(a=>'['+a.toLowerCase()+a.toUpperCase()+']')
  15.             .join('');
  16.         return new RegExp('((?:^| )'+ modifiedStr + ' +)([A-Z!%$#]{8,})([ .,]|$)','g');
  17.     }
  18.  
  19.     let output =[];
  20.     let specialKey = input.shift();
  21.     let matcher = createMatcher(specialKey);
  22.     for (let line of input){
  23.         let modifiedLine = line;
  24.         while(match = matcher.exec(line)){
  25.             modifiedLine = modifiedLine
  26.                 .replace(match[0], match[1] + decode(match[2])+match[3]);
  27.         }
  28.  
  29.         output.push(modifiedLine);
  30.     }
  31.  
  32.     console.log(output.join('\n'));
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement