File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 1+ require ( './solution' )
You can’t perform that action at this time.
0 commit comments