Skip to content

Commit 9421e73

Browse files
feat(2021-day-06): count the intersections when including diagonals
solves part 2
1 parent 6adb95e commit 9421e73

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

2021/day-05/solution.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,35 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
1313
return JSON.parse(JSON.stringify(initData))
1414
}
1515

16-
const part1 = () => {
16+
const task = (supportDiagonals) => {
1717
const data = resetInput()
18+
1819
// Allocate map
1920
const max = data.reduce((max, curr) => {
2021
max[0] = Math.max(max[0], curr[0], curr[2]) // find the maximum X value
2122
max[1] = Math.max(max[1], curr[1], curr[3]) // find the maximum Y value
2223
return max
2324
}, [0, 0])
2425

25-
console.debug(max)
26-
2726
let map = [...new Array(max[1] + 1)].map(() => {
2827
return [...new Array(max[0] + 1)].map(() => 0)
2928
})
3029

3130
data.forEach((line) => {
32-
map = chartLine(map, ...line)
31+
map = chartLine(map, ...line, supportDiagonals)
3332
})
3433

3534
return countIntersections(map, 2)
3635
}
3736

37+
const part1 = () => {
38+
return task(false)
39+
}
40+
3841
const part2 = () => {
39-
const data = resetInput()
40-
console.debug(data)
41-
return 'No answer yet'
42+
return task(true)
4243
}
44+
4345
const answers = []
4446
answers.push(part1())
4547
answers.push(part2())

0 commit comments

Comments
 (0)