DEV Community

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

Posted on

Advent of code - Day 6

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.

For day #6, I got super motivated. I stayed awake until the challenge release (6am in my timezone), so I could maybe manage to get ranked!

After 7 minutes, I submitted the part 1 (pretty good score IMO!)... Only to realise that most of the top 100 took less than a minute! I have no idea how they do this, but I'm still happy with my performance.

Anyway, here is my solution for day #6:

input
  .split(/\r?\n\r?\n/)
  .map((group) => group.split("\n"))
  .map((group) => group.filter(Boolean).map((answers) => answers.split("")))
  .map((group) => {
    const record: Record<string, number> = {};
    group.map((answers) => {
      answers.map((answer) => {
        record[answer] = record[answer] + 1 || 1;
      });
    });
    return Object.keys(record).filter((x) => record[x] === group.length)
      .length;
  })
  .reduce((acc, v) => acc + v, 0);
Enter fullscreen mode Exit fullscreen mode

Feel free to share yours in the comments!


Photo by Markus Spiske on Unsplash

Top comments (0)