Skip to content

Commit 6adb95e

Browse files
feat(2021-day-05): support charting diagonal lines on vent map
1 parent 50f064d commit 6adb95e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

2021/day-05/vents.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @param {*} y2 end of line vertical point
88
* @returns
99
*/
10-
const chartLine = (data, x1, y1, x2, y2) => {
10+
const chartLine = (data, x1, y1, x2, y2, allowDiaganol = false) => {
1111
let x = x1
1212
let y = y1
1313
if (y1 === y2) {
@@ -28,6 +28,17 @@ const chartLine = (data, x1, y1, x2, y2) => {
2828
y += yDir
2929
}
3030
data[y][x]++ // coordinates are inclusive
31+
} else if (allowDiaganol) {
32+
// chart diagonal line
33+
console.debug(`Drawing diagonal line ${x1},${y1} to ${x2},${y2}`)
34+
const xDir = (x2 > x1) ? 1 : -1
35+
const yDir = (y2 > y1) ? 1 : -1
36+
while (x !== x2 && y !== y2) {
37+
data[y][x]++
38+
x += xDir
39+
y += yDir
40+
}
41+
data[y][x]++ // coordinates are inclusive
3142
} else {
3243
console.debug(`Skipping diagonal line ${x1},${y1} to ${x2},${y2}`)
3344
}

2021/day-05/vents.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ const sampleMap = `.......1..
2424
..........
2525
222111....`
2626

27+
const sampleDiagonalMap = `1.1....11.
28+
.111...2..
29+
..2.1.111.
30+
...1.2.2..
31+
.112313211
32+
...1.2....
33+
..1...1...
34+
.1.....1..
35+
1.......1.
36+
222111....`
37+
2738
const parsedTestData = parseLines(testData)
2839

2940
describe('--- Day 5: Hydrothermal Venture ---', () => {
@@ -84,4 +95,19 @@ describe('--- Day 5: Hydrothermal Venture ---', () => {
8495
})
8596
})
8697
})
98+
describe('Part 2', () => {
99+
describe('chartLine()', () => {
100+
it('includes diagonal lines when specified', () => {
101+
// 10x10 empty grid
102+
let data = [...new Array(10)].map(() => {
103+
return [...new Array(10)].map(() => 0)
104+
})
105+
// Map some lines
106+
parsedTestData.forEach((row) => {
107+
data = chartLine(data, ...row, true)
108+
})
109+
expect(render(data)).to.equal(sampleDiagonalMap)
110+
})
111+
})
112+
})
87113
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy