DEV Community

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

Posted on

2 2

Advent of code - Day 10

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.

I'm already happy I managed to finish it. I know there are some incredible solutions out there (a colleague of mine solved is with a math equation 🤯), but all I could do was a brute-force. As I often say, IIWIW (If It Works It Works)!

Here is my solution for day #10:

const cache = [];

function valueFor(code, currentValue) {
  if (cache[currentValue]) return cache[currentValue];
  cache[currentValue] = resolve(code, currentValue);
  return cache[currentValue];
}

function resolve(code, currentValue = 0) {
  if (!code.length) return 1;
  const [first, second, third] = code;
  let result = 0;
  if (first - currentValue > 0 && first - currentValue <= 3)
    result += valueFor(code.slice(1), first);
  if (second - currentValue > 0 && second - currentValue <= 3)
    result += valueFor(code.slice(2), second);
  if (third - currentValue > 0 && third - currentValue <= 3)
    result += valueFor(code.slice(3), third);
  return result;
}

resolve(input)
Enter fullscreen mode Exit fullscreen mode

Feel free to share your solution in the comments!


Photo by Markus Spiske on Unsplash

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

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