summaryrefslogtreecommitdiffstats
path: root/day10/day10-2.js
blob: 83435bf68e0254ac135b1513a373e5dd6acec121 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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))