DEV Community

Thomas Junkツ
Thomas Junkツ

Posted on

Advent of code Day 1 - Javascript while loop abuse

Yesterday I found a little time, to start the annual AOC.

Yes, I know, I am late to the party. But hey: Better late than never.

So, the first part was an easy warm up, just sum an array. Part 2 was a bit trickier. I came up with this - admittedly - somewhat "not clean" solution.

OTOH, I find it nonetheless readable in one way or the other.

const findDoubleFrequency = array => {
  const doubles = new Set();
  doubles.add(0);
  let acc = 0;
  while (
    !array.find(x => {
      acc += x;
      if (doubles.has(acc)) {
        return true;
      }
      doubles.add(acc);
      return false;
    })
  ) {}
  return acc;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More