Skip to content

Commit 1a867cc

Browse files
feat(2021-day-05): count the intersections on the map that exceed a threshold of lines
1 parent a20faca commit 1a867cc

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

2021/day-05/vents.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ const chartLine = (data, x1, y1, x2, y2) => {
3434
return data
3535
}
3636

37+
/**
38+
* Count the number of points with more than [threshold] intersecting lines
39+
* @param {*} data
40+
* @param {*} threshold
41+
* @returns
42+
*/
43+
const countIntersections = (data, threshold) => {
44+
return data.reduce((total, row) => {
45+
total += row.filter((cell) => (cell >= threshold)).length
46+
return total
47+
}, 0)
48+
}
49+
3750
/**
3851
* Creates a visible map from the data
3952
* @param {*} data
@@ -63,5 +76,6 @@ const parseLines = (data) => {
6376
module.exports = {
6477
render,
6578
chartLine,
79+
countIntersections,
6680
parseLines
6781
}

2021/day-05/vents.test.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { render, chartLine, parseLines } = require('./vents')
3+
const { render, chartLine, parseLines, countIntersections } = require('./vents')
44

55
const testData = `0,9 -> 5,9
66
8,0 -> 0,8
@@ -58,17 +58,30 @@ describe('--- Day 5: Hydrothermal Venture ---', () => {
5858
data = chartLine(data, 3, 4, 1, 4)
5959
expect(render(data)).to.equal(sampleMap)
6060
})
61-
})
62-
it('skips diagonal lines', () => {
63-
// 10x10 empty grid
64-
let data = [...new Array(10)].map(() => {
65-
return [...new Array(10)].map(() => 0)
61+
it('skips diagonal lines', () => {
62+
// 10x10 empty grid
63+
let data = [...new Array(10)].map(() => {
64+
return [...new Array(10)].map(() => 0)
65+
})
66+
// Map some lines
67+
parsedTestData.forEach((row) => {
68+
data = chartLine(data, ...row)
69+
})
70+
expect(render(data)).to.equal(sampleMap)
6671
})
67-
// Map some lines
68-
parsedTestData.forEach((row) => {
69-
data = chartLine(data, ...row)
72+
})
73+
describe('countIntersections()', () => {
74+
it('counts how many intersections exist of (n) lines or more', () => {
75+
// 10x10 empty grid
76+
let data = [...new Array(10)].map(() => {
77+
return [...new Array(10)].map(() => 0)
78+
})
79+
// Map some lines
80+
parsedTestData.forEach((row) => {
81+
data = chartLine(data, ...row)
82+
})
83+
expect(countIntersections(data, 2)).to.equal(5)
7084
})
71-
expect(render(data)).to.equal(sampleMap)
7285
})
7386
})
7487
})

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