DEV Community

Cover image for Advent of code - Day 19
Quentin Ménoret
Quentin Ménoret

Posted on

Advent of code - Day 19

Are you participating in the Advent of code this year?

If you don't know what the advent of code is, it's a website where you'll find a daily challenge (every day it gets harder). It's a really fun event, you should participate!

I try to solve the exercises using either JavaScript or TypeScript and will share my solutions daily (with one day delay so no one can cheat!). I only share the solution for the second part.

Again a fun one where you have to generate a RegEx to validate some strings. Part one was super nice, I just had a function to generate the RegEx and... voilà. For part 2, I had to hardcode the two rules for 11 and 8, then merge then for the RegEx to work. Not beautiful, but it works!

Here is my solution for day #19:

const [rawRules, input] = rawInput.split('\n\n')

const rules = rawRules
  .map((rule) => rule.split(': '))
  .map((rule) => {
    return {
      id: rule[0],
      possibilities: rule[1],
  .reduce((acc, rule) => {
    acc[] = rule
    return acc
  }, {})

function transformToRegex(rules, possibilities) {
  if (possibilities === '42 | 42 8') return `(${transformToRegex(rules, rules[42].possibilities)})+`
  return possibilities
    .split(' | ')
    .map((possibility) => {
      return possibility
        .split(' ')
        .map((p) => {
          if (p.match(/\".+\"/)) return p.slice(1, -1)
          const expression = transformToRegex(rules, rules[p].possibilities)
          return expression.includes('|') ? `(${expression})` : expression

const rule11Part1 = transformToRegex(rules, '42')
const rule11Part2 = transformToRegex(rules, '31')

let rule11 = `${rule11Part1}${rule11Part2}`
for (let i = 0; i < 20; i++) {
  rule11 = `${rule11Part1}(${rule11})?${rule11Part2}`

const firstRegex = transformToRegex(rules, rules[8].possibilities)
const fullRegex = new RegExp(`^${firstRegex}${rule11}$`)

console.log(input.split('\n').filter((str) => str.match(fullRegex)).length)
Enter fullscreen mode Exit fullscreen mode

Feel free to share your solution in the comments!

Photo by Markus Spiske on Unsplash

Discussion (0)