From 468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d Mon Sep 17 00:00:00 2001 From: Piotr Russ Date: Thu, 10 Dec 2020 20:54:28 +0100 Subject: day10 --- day10/day10-2.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 day10/day10-2.js (limited to 'day10/day10-2.js') diff --git a/day10/day10-2.js b/day10/day10-2.js new file mode 100644 index 0000000..83435bf --- /dev/null +++ b/day10/day10-2.js @@ -0,0 +1,19 @@ +const data = require('./data') +const sorted = data.concat(0).sort((a,b) => a - b) +sorted.push(sorted[sorted.length-1] + 3) +const paths = [] + +const count = (index) => { + if (index === sorted.length - 1) { return 1 } + if (paths[index]) { return paths[index] } + + let pathsCount = 0 + sorted + .filter(s => s > sorted[index] && s <= sorted[index] + 3) + .forEach(f => pathsCount += count(sorted.indexOf(f))) + paths[index] = pathsCount + return pathsCount +} + +console.log(count(0)) + -- cgit v1.2.3