DEV Community

loading...
Cover image for Advent of code - Day 5

Advent of code - Day 5

qmenoret profile image Quentin Ménoret ・1 min read

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 #5, I starter with such complicated solutions... Took me a while to realise that this one was probably the easier we had to tackle since the beginning!

In the end, no need to do any binary operation. You just need to filter an array:

const allIds = tickets.map((ticket) =>
  parseInt(
    ticket
      .split("")
      .map((char) => (char === "B" || char === "R" ? 1 : 0))
      .join(""),
    2
  )
);
const min = Math.min(...allIds);
const max = Math.max(...allIds);
const possibilities = Array.from(Array(max).keys()).slice(min);
const [seat] = possibilities.filter((p) => !allIds.includes(p));
Enter fullscreen mode Exit fullscreen mode

Feel free to share yours in the comments!


Photo by Markus Spiske on Unsplash

Discussion (0)

pic
Editor guide