Skip to content

Commit

Permalink
feat(2021-day-06): count the intersections when including diagonals
Browse files Browse the repository at this point in the history
solves part 2
  • Loading branch information
amclin committed Dec 14, 2021
1 parent 6adb95e commit 9421e73
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions 2021/day-05/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
return JSON.parse(JSON.stringify(initData))
}

const part1 = () => {
const task = (supportDiagonals) => {
const data = resetInput()

// Allocate map
const max = data.reduce((max, curr) => {
max[0] = Math.max(max[0], curr[0], curr[2]) // find the maximum X value
max[1] = Math.max(max[1], curr[1], curr[3]) // find the maximum Y value
return max
}, [0, 0])

console.debug(max)

let map = [...new Array(max[1] + 1)].map(() => {
return [...new Array(max[0] + 1)].map(() => 0)
})

data.forEach((line) => {
map = chartLine(map, ...line)
map = chartLine(map, ...line, supportDiagonals)
})

return countIntersections(map, 2)
}

const part1 = () => {
return task(false)
}

const part2 = () => {
const data = resetInput()
console.debug(data)
return 'No answer yet'
return task(true)
}

const answers = []
answers.push(part1())
answers.push(part2())
Expand Down

0 comments on commit 9421e73

Please sign in to comment.