horvathm

Day 19 Part 1

Dec 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. let [gram, data] = fs.readFileSync('input', 'utf8').trim().split('\n\n');
  4.  
  5. gram = gram.split('\n').map(l => {
  6.         const m = l.match(/(\d+): "?([^"]*)"?/);
  7.         return { n: m[1], t: m[2] }
  8.     }, {});
  9.  
  10. //console.log(gram);
  11. gram = gram.sort((a, b) => b.n - a.n);
  12. //console.log(gram);
  13.  
  14. for (const rule of gram) {
  15.     for (const line of gram) {
  16.         line.t = line.t.replace(new RegExp(rule.n, 'g'), `(${rule.t})`);
  17.     }
  18.     // console.log('-----');
  19.     // console.log(gram);
  20. }
  21.  
  22. //console.log(gram);
  23.  
  24. let rule0 = gram[gram.length - 1].t;
  25.  
  26. //rule0 = rule0.replace(/\(a\)/g, 'a');
  27. //rule0 = rule0.replace(/\(b\)/g, 'a');
  28. rule0 = rule0.replace(/ /g, '')
  29.  
  30. const re = new RegExp(`^${rule0}$`)
  31.  
  32. console.log(re);
  33.  
  34. data = data.split('\n');
  35. for (const line of data) {
  36.     if (re.test(line)) {
  37.         console.log('passed', line);
  38.     } else {
  39.         console.log('failed', line);
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment