diff options
author | 2020-12-10 20:54:28 +0100 | |
---|---|---|
committer | 2020-12-10 20:54:28 +0100 | |
commit | 468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d (patch) | |
tree | deb1871886c5f24ca58529c35340ff896d0dcecc /day10/day10-2.js | |
parent | d0588d654684a5a6d5dfec3b983cb4f41d057ca6 (diff) | |
download | advent_of_code_2020-468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d.tar.gz advent_of_code_2020-468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d.tar.bz2 advent_of_code_2020-468e261bccd8fc44ee6ffd3e7fda1c3659a7ab1d.zip |
day10
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)) + |