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
  .split('\n')
  .map((rule) => rule.split(': '))
  .map((rule) => {
    return {
      id: rule[0],
      possibilities: rule[1],
    }
  })
  .reduce((acc, rule) => {
    acc[rule.id] = 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
        })
        .join('')
    })
    .join('|')
}

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay