diff options
Diffstat (limited to 'day10/day10-2.js')
-rw-r--r-- | day10/day10-2.js | 19 |
1 files changed, 19 insertions, 0 deletions
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)) + |