Skip to content

Commit a20faca

Browse files
feat(2021-day-05): map orthagonal lines for hydrothermal vents
1 parent 8d9336d commit a20faca

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

2021/day-05/vents.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
*
3+
* @param {*} data Existing coordinate data
4+
* @param {*} x1 start of line horizontal point
5+
* @param {*} y1 start of line vertical point
6+
* @param {*} x2 end of line horizontal point
7+
* @param {*} y2 end of line vertical point
8+
* @returns
9+
*/
10+
const chartLine = (data, x1, y1, x2, y2) => {
11+
let x = x1
12+
let y = y1
13+
if (y1 === y2) {
14+
// chart horizontal line
15+
console.debug(`Drawing horizontal line ${x1},${y1} to ${x2},${y2}`)
16+
const xDir = (x2 > x1) ? 1 : -1
17+
while (x !== x2) {
18+
data[y][x]++
19+
x += xDir
20+
}
21+
data[y][x]++ // coordinates are inclusive
22+
} else if (x1 === x2) {
23+
// chart vertical line
24+
console.debug(`Drawing vertical line ${x1},${y1} to ${x2},${y2}`)
25+
const yDir = (y2 > y1) ? 1 : -1
26+
while (y !== y2) {
27+
data[y][x]++
28+
y += yDir
29+
}
30+
data[y][x]++ // coordinates are inclusive
31+
} else {
32+
console.debug(`Skipping diagonal line ${x1},${y1} to ${x2},${y2}`)
33+
}
34+
return data
35+
}
36+
37+
/**
38+
* Creates a visible map from the data
39+
* @param {*} data
40+
* @returns string
41+
*/
42+
const render = (data) => {
43+
return data
44+
.map((row) => row.join(''))
45+
.join('\n')
46+
.replace(/0/gi, '.')
47+
}
48+
49+
/**
50+
* Parses the provided data rules into useable lines
51+
* @param string data
52+
* @returns Array
53+
*/
54+
const parseLines = (data) => {
55+
return data.split('\n')
56+
.map(
57+
(row) => row.replace(' -> ', ',')
58+
.split(',')
59+
.map((val) => parseInt(val))
60+
)
61+
}
62+
63+
module.exports = {
64+
render,
65+
chartLine,
66+
parseLines
67+
}

2021/day-05/vents.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { render, chartLine, parseLines } = require('./vents')
4+
5+
const testData = `0,9 -> 5,9
6+
8,0 -> 0,8
7+
9,4 -> 3,4
8+
2,2 -> 2,1
9+
7,0 -> 7,4
10+
6,4 -> 2,0
11+
0,9 -> 2,9
12+
3,4 -> 1,4
13+
0,0 -> 8,8
14+
5,5 -> 8,2`
15+
16+
const sampleMap = `.......1..
17+
..1....1..
18+
..1....1..
19+
.......1..
20+
.112111211
21+
..........
22+
..........
23+
..........
24+
..........
25+
222111....`
26+
27+
const parsedTestData = parseLines(testData)
28+
29+
describe('--- Day 5: Hydrothermal Venture ---', () => {
30+
describe('Part 1', () => {
31+
describe('render()', () => {
32+
it('displays a visual map of the vents', () => {
33+
expect(render([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
34+
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
35+
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
36+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
37+
[0, 1, 1, 2, 1, 1, 1, 2, 1, 1],
38+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
39+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
40+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
41+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
42+
[2, 2, 2, 1, 1, 1, 0, 0, 0, 0]]))
43+
.to.equal(sampleMap)
44+
})
45+
})
46+
describe('chartLine()', () => {
47+
it('charts a line between two points', () => {
48+
// 10x10 empty grid
49+
let data = [...new Array(10)].map(() => {
50+
return [...new Array(10)].map(() => 0)
51+
})
52+
// Map some horizontal and vertical lines
53+
data = chartLine(data, 0, 9, 5, 9)
54+
data = chartLine(data, 9, 4, 3, 4)
55+
data = chartLine(data, 2, 2, 2, 1)
56+
data = chartLine(data, 7, 0, 7, 4)
57+
data = chartLine(data, 0, 9, 2, 9)
58+
data = chartLine(data, 3, 4, 1, 4)
59+
expect(render(data)).to.equal(sampleMap)
60+
})
61+
})
62+
it('skips diagonal lines', () => {
63+
// 10x10 empty grid
64+
let data = [...new Array(10)].map(() => {
65+
return [...new Array(10)].map(() => 0)
66+
})
67+
// Map some lines
68+
parsedTestData.forEach((row) => {
69+
data = chartLine(data, ...row)
70+
})
71+
expect(render(data)).to.equal(sampleMap)
72+
})
73+
})
74+
})

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