Skip to content

Commit 2ce56c4

Browse files
Merge pull request #124 from amclin/feat/2020-day-03
Feat/2020 day 03
2 parents 072038c + dfbfec2 commit 2ce56c4

File tree

5 files changed

+443
-0
lines changed

5 files changed

+443
-0
lines changed

2020/day-03/airportRoute.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const queryPosition = ({ map, position }) => {
2+
// check vertical position
3+
const row = map.rows[position[1]]
4+
// check horizontal which repeats
5+
return row[(position[0] % row.length)]
6+
}
7+
8+
const positionHasTree = ({ map, position }) => {
9+
return (queryPosition({ map, position }) === '#')
10+
}
11+
12+
const countTreesOnRoute = ({
13+
map,
14+
start = [0, 0],
15+
slope = [3, 1]
16+
}) => {
17+
let treeCount = 0
18+
const position = start
19+
const advance = () => {
20+
position[0] += slope[0]
21+
position[1] += slope[1]
22+
}
23+
while (position[1] < map.rows.length) {
24+
if (positionHasTree({ map, position })) treeCount++
25+
advance()
26+
}
27+
return treeCount
28+
}
29+
30+
module.exports = {
31+
countTreesOnRoute,
32+
positionHasTree
33+
}

2020/day-03/airportRoute.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { positionHasTree, countTreesOnRoute } = require('./airportRoute')
4+
const testData = [
5+
'..##.......',
6+
'#...#...#..',
7+
'.#....#..#.',
8+
'..#.#...#.#',
9+
'.#...##..#.',
10+
'..#.##.....',
11+
'.#.#.#....#',
12+
'.#........#',
13+
'#.##...#...',
14+
'#...##....#',
15+
'.#..#...#.#'
16+
]
17+
18+
describe('--- Day 3: Toboggan Trajectory ---', () => {
19+
describe('Part 1', () => {
20+
describe('positionHasTree()', () => {
21+
it('can find a tree', () => {
22+
expect(positionHasTree({ map: { rows: testData }, position: [4, 5] })).to.equal(true)
23+
expect(positionHasTree({ map: { rows: testData }, position: [8, 9] })).to.equal(false)
24+
})
25+
it('can handle the horizontal terrain repeat', () => {
26+
expect(positionHasTree({ map: { rows: testData }, position: [25, 5] })).to.equal(false)
27+
})
28+
})
29+
describe('countTreesOnRoute()', () => {
30+
it('tallies the number of trees on the route', () => {
31+
expect(
32+
countTreesOnRoute({
33+
map: { rows: testData }
34+
})
35+
).to.equal(7)
36+
})
37+
})
38+
})
39+
})

2020/day-03/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./solution')

0 commit comments

Comments
 (0)