Part 2 javscript solution 🎨:
const fs = require('fs').promises; const path = require('path'); const main = async () => { const input = await fs.readFile(path.join(__dirname, './10.txt'), 'utf-8'); let lines = input.split('\n').map((x) => parseInt(x)); const cache = {}; lines.push(0, Math.max(...lines) + 3); lines = lines.sort((a, b) => a - b); const dp = (n) => { if (n === lines.length - 1) { return 1; } if (n in cache) { return cache[n]; } let ans = 0; for (let i = n + 1; i < lines.length; i++) { if (lines[i] - lines[n] <= 3) { ans += dp(i); } } cache[n] = ans; return ans; }; console.log(dp(0)); }; main();
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Part 2 javscript solution 🎨: