Skip to content

Commit

Permalink
feat(2021-day-05): count the intersections on the map that exceed a t…
Browse files Browse the repository at this point in the history
…hreshold of lines
  • Loading branch information
amclin committed Dec 14, 2021
1 parent a20faca commit 1a867cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
14 changes: 14 additions & 0 deletions 2021/day-05/vents.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ const chartLine = (data, x1, y1, x2, y2) => {
return data
}

/**
* Count the number of points with more than [threshold] intersecting lines
* @param {*} data
* @param {*} threshold
* @returns
*/
const countIntersections = (data, threshold) => {
return data.reduce((total, row) => {
total += row.filter((cell) => (cell >= threshold)).length
return total
}, 0)
}

/**
* Creates a visible map from the data
* @param {*} data
Expand Down Expand Up @@ -63,5 +76,6 @@ const parseLines = (data) => {
module.exports = {
render,
chartLine,
countIntersections,
parseLines
}
33 changes: 23 additions & 10 deletions 2021/day-05/vents.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { render, chartLine, parseLines } = require('./vents')
const { render, chartLine, parseLines, countIntersections } = require('./vents')

const testData = `0,9 -> 5,9
8,0 -> 0,8
Expand Down Expand Up @@ -58,17 +58,30 @@ describe('--- Day 5: Hydrothermal Venture ---', () => {
data = chartLine(data, 3, 4, 1, 4)
expect(render(data)).to.equal(sampleMap)
})
})
it('skips diagonal lines', () => {
// 10x10 empty grid
let data = [...new Array(10)].map(() => {
return [...new Array(10)].map(() => 0)
it('skips diagonal lines', () => {
// 10x10 empty grid
let data = [...new Array(10)].map(() => {
return [...new Array(10)].map(() => 0)
})
// Map some lines
parsedTestData.forEach((row) => {
data = chartLine(data, ...row)
})
expect(render(data)).to.equal(sampleMap)
})
// Map some lines
parsedTestData.forEach((row) => {
data = chartLine(data, ...row)
})
describe('countIntersections()', () => {
it('counts how many intersections exist of (n) lines or more', () => {
// 10x10 empty grid
let data = [...new Array(10)].map(() => {
return [...new Array(10)].map(() => 0)
})
// Map some lines
parsedTestData.forEach((row) => {
data = chartLine(data, ...row)
})
expect(countIntersections(data, 2)).to.equal(5)
})
expect(render(data)).to.equal(sampleMap)
})
})
})

0 comments on commit 1a867cc

Please sign in to comment.