DEV Community

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

Posted on

1

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)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

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

Okay