From 6e98070f918d9ba6213097806c9b6c5ecf9f3b63 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Sun, 6 Dec 2020 21:11:18 -0800 Subject: [PATCH 1/3] feat[2020-day-03]: check a route to the airport for trees --- 2020/day-03/airportRoute.js | 33 +++++++++++++++++++++++++++ 2020/day-03/airportRoute.test.js | 39 ++++++++++++++++++++++++++++++++ 2020/day-03/index.js | 1 + 3 files changed, 73 insertions(+) create mode 100644 2020/day-03/airportRoute.js create mode 100644 2020/day-03/airportRoute.test.js create mode 100644 2020/day-03/index.js diff --git a/2020/day-03/airportRoute.js b/2020/day-03/airportRoute.js new file mode 100644 index 0000000..7b4743c --- /dev/null +++ b/2020/day-03/airportRoute.js @@ -0,0 +1,33 @@ +const queryPosition = ({ map, position }) => { + // check vertical position + const row = map.rows[position[1]] + // check horizontal which repeats + return row[(position[0] % row.length)] +} + +const positionHasTree = ({ map, position }) => { + return (queryPosition({ map, position }) === '#') +} + +const countTreesOnRoute = ({ + map, + start = [0, 0], + slope = [3, 1] +}) => { + let treeCount = 0 + const position = start + const advance = () => { + position[0] += slope[0] + position[1] += slope[1] + } + while (position[1] < map.rows.length) { + if (positionHasTree({ map, position })) treeCount++ + advance() + } + return treeCount +} + +module.exports = { + countTreesOnRoute, + positionHasTree +} diff --git a/2020/day-03/airportRoute.test.js b/2020/day-03/airportRoute.test.js new file mode 100644 index 0000000..ba640a5 --- /dev/null +++ b/2020/day-03/airportRoute.test.js @@ -0,0 +1,39 @@ +/* eslint-env mocha */ +const { expect } = require('chai') +const { positionHasTree, countTreesOnRoute } = require('./airportRoute') +const testData = [ + '..##.......', + '#...#...#..', + '.#....#..#.', + '..#.#...#.#', + '.#...##..#.', + '..#.##.....', + '.#.#.#....#', + '.#........#', + '#.##...#...', + '#...##....#', + '.#..#...#.#' +] + +describe('--- Day 3: Toboggan Trajectory ---', () => { + describe('Part 1', () => { + describe('positionHasTree()', () => { + it('can find a tree', () => { + expect(positionHasTree({ map: { rows: testData }, position: [4, 5] })).to.equal(true) + expect(positionHasTree({ map: { rows: testData }, position: [8, 9] })).to.equal(false) + }) + it('can handle the horizontal terrain repeat', () => { + expect(positionHasTree({ map: { rows: testData }, position: [25, 5] })).to.equal(false) + }) + }) + describe('countTreesOnRoute()', () => { + it('tallies the number of trees on the route', () => { + expect( + countTreesOnRoute({ + map: { rows: testData } + }) + ).to.equal(7) + }) + }) + }) +}) diff --git a/2020/day-03/index.js b/2020/day-03/index.js new file mode 100644 index 0000000..a7e4223 --- /dev/null +++ b/2020/day-03/index.js @@ -0,0 +1 @@ +require('./solution') From de18ecd89489a1d42480f0c6efb61d463efb4740 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Sun, 6 Dec 2020 21:16:02 -0800 Subject: [PATCH 2/3] feat[2020-day-03]: find number of tree collisions on provided route Solution for part 1 --- 2020/day-03/input.txt | 323 ++++++++++++++++++++++++++++++++++++++++ 2020/day-03/solution.js | 35 +++++ 2 files changed, 358 insertions(+) create mode 100644 2020/day-03/input.txt create mode 100644 2020/day-03/solution.js diff --git a/2020/day-03/input.txt b/2020/day-03/input.txt new file mode 100644 index 0000000..086d326 --- /dev/null +++ b/2020/day-03/input.txt @@ -0,0 +1,323 @@ +...........#..#.#.###....#..... +...#..#...........#.#...#...... +#.....#..#........#...#..##.... +..#...##.#.....#.............#. +#.#..#......#.....#....#....... +.....#......#..#....#.....#.... +.......#.#..............#...... +.....#...#..........##...#..... +...#....#.#...#.#........#...#. +..#.........###.......##...##.. +.#....#...........#........#..# +..#.............##............. +..#.##.#....#................#. +.....##.#.......#....#...#..... +......#.#....##................ +..#..........###..#..#.#..#.... +....#..............#....##..#.# +.#.........#.#....#.#.#....#... +..#.....#......##.#....#....... +..#.#....#..#.#...##....###.... +...#......##...#........#.#..#. +.##.#.......##....#............ +...##..#.#............#...#.#.. +.##...##.#..#.................. +..#......##......#......##..... +.....##...#..#...#.........#... +.##.#.....#..#..#.##....##....# +..#.#......#.......##.......... +......................#......## +##.#...#.................#.#.#. +......#.#..........#.....##.#.. +#.#......#.....#...........#... +.....#...#.......#..#..#.#...#. +...........#......#.#...#...... +....##...##...........#......#. +.........#.##.................. +......#...#....#......##.##...# +......#...#.#########......#... +.......#.#...#.......#..#...... +............#...#...#.###...##. +...........#..........#...#.... +...#..#.#................#.#..# +..#....#.....#.............#.#. +....##......#........#....#.... +........##...............#....# +........#..#...#..............# +...#....#.#...#..#...#....#.#.# +.........#.......#....##....... +#.##..............#.#........## +......................###...... +.........#....##..##....#.#.#.. +.#...##.#.#...#....##..#.....#. +....................#.#......#. +.#..#.......................#.. +..#..#.............#..#....#... +...#...#..#...#...#.#..#.##.... +........#....#...#....#........ +.#.....#........#.........#...# +...#......#..#..#..####..#..... +#....#..............#.##....... +.#....#.............##...#..... +....#...#.##........##......#.. +##....#...#.......#..#........# +....##........................# +..................#..#......... +..#....#........#..#.......#... +#...#..#....#...##...........#. +.........#..#..#.#.##.......... +....#.#..#.....#..#.#.#.#..#.## +##................#.##.....#... +.#.....###..#..#..#.....#....## +...#...........#..........####. +.#.....#....#......#.##..#.#... +..#...##....#................#. +........#.......#......#.#..... +....#.#.#.#....#...#......#..#. +...........#......#..#......... +###...##......##.#..#....##.... +##....##.........#..#....###... +#.#.....#....#......#...#..##.. +#....##.#..............#.##.... +.#........#.#.........#...#.... +......................#......#. +........#..#..##.....#..#.#.... +..#...###.................#..#. +...#...#............#.......... +.##.......#..#.........#....#.. +.#..............#....#....##... +...............##..#.#.......## +.#.....#....#...#..#.......#..# +#..#.............#....#......#. +.....#.#......#.........###..#. +.#...#.#...............#....#.. +#......#.............#......... +.#.##.#.####...#..#.##..#.....# +.....#......#..#...#.......#... +#........###...#.....#..#.....# +....#.#.....#...#..........#... +...#.#.......#.#......#..#.##.. +..#..........#.#..#.......#.#.. +#...#.#..............#...###.#. +...#..#...#............###..... +..#..#...#.#............#..#... +.###.#.....................#..# +....#....#..#.....##.##........ +#....#....#.#..#.........#..... +.#.....##....#............##..# +#....#.#...#...#..#.#......#... +#.....##.....##.....##.#...##.. +...##...#..#..####.#........#.. +.........#...#......##..##..#.. +..#.....###.#..#..####.#....... +.......#......#...#..##....#... +.#.....#.#.#..#....#...#..##... +..........#.#...#...#.#..#..... +....#.....#........#.....##..#. +..#.#.##.........#...##.....##. +.........#.##....#............# +............##.....#.......#.#. +......#.....#...#..#..###...... +##.....#.......#...##.#....#... +...........##......#..##...#.#. +..#.#.#.#...#.......#....#...#. +#.............#.....#.#...###.. +##....#.......#.....#..##.#.... +...#.......#....#.........##... +......#.......#......##.##..... +..#......#...#.#........#...... +....#.#....#.#.##......#.....#. +#......#.........#..#....#..... +........#..#....##.....#....... +#......##....#.##....#......#.. +..#.......#............##.....# +...........#...#...........#... +#.......#...#....#....#...#.#.# +..###..#........#........#..... +..#..###...........#.#......#.. +.#...........#......#.......... +.#.......#.....#.......#....... +..#......##.#............#....# +#..........#.....#.#..#........ +.....#...##.##.......#......#.. +..........#.....#........#.#.#. +....#......#.....#......#.#.... +.........#.#.#..#...##....#...# +.........#.......#...##...#.#.. +.##........#...............#... +.......#....#...........##..... +.........###......#.........#.# +......#.......#...#..........#. +...#.#..........##......#...#.. +#.......#.#..........#......... +................#..#......#..## +.....#...#....#.#.....#........ +#.....#....#...........#....#.. +#....#.#..#...##....#...##.#... +...#.....#......#.#....#..#..#. +..#................#...#.#..##. +..........#..............#..#.# +.....##.....#..#.###........... +....#.#......#.#...........#... +.#....#.#.........##.#....#.... +.#.#........#........###....#.. +##.#................#...#..#... +.......#......##..#.....#..#.#. +...#............#......###...## +#.#...........#.........#...... +.....#.#.#.................#... +....#..............#...#.#..... +...#.#.....##..#............... +.#..##...#....##.....###..#.... +...............#...#...#.#.###. +.###....#.....#...#.#......#... +...#..#.....#.......#..##.#.... +...........#..#....##..#...#... +...#...#..........#.......##.#. +............#.#.......#........ +....#.........#.....#.......... +...#.###.##..#...##..####..#..# +.#.#...#..#...................# +.....#..#.....##..#............ +....#......#...##..#.##........ +...#...............#..#.....##. +...#......#.........#.#..##.... +.#....#.##.......#......#...... +....#.......#....#..........#.. +#.#.#....###.#.#.............#. +..##..###........#.#..#.#..#... +......#.#............##.#...### +.........#.#....#####.....##... +............##......#.#..#..... +...#.....#.....###....#........ +##..........####.##.#.#........ +....................##.....##.# +#.#............#........#...... +....#...##.....#......#....#... +...###.#..##..................# +..###......#..............#.#.# +.#...#...........#....#........ +....#............#..#..#.#..... +...#.........#.#.........#.#### +..#...#...#...#...........#.... +...............#.#......##..#.. +#....#.#.......#.#..#......#..# +........#.#....#..#...#..#...#. +...#..#.......#...........#.... +...........#.......#........... +.#......#................#..... +....#.#.#...#......#..#...#.... +................#.#.#....#..... +.........................##..#. +.#...........#............##... +#...............#.....##.#.#..# +.........#.......###....#.....# +....##...#...#.....#..#........ +........#.....#..#.#.#...#..#.. +......#.......#.#.........#.#.. +#......#............#...#....#. +#..##...#..#................#.. +.##...#...#.....#.##.......#..# +.......#.##........##..##...... +##.#..##...............#.....#. +......#....#..##...#......###.# +#........##..#....#.#......#... +.#......##.#...#.#...#......... +.#.#...#..#.............#...... +.##..........#..........#...... +.#.....#.....#..............#.# +..#.........#..#.#.....#.#....# +..#.##..............##...#..### +....................#.......... +......###..#..#...........#.... +..#..........#.......#...#..... +...#......#......#............. +....##..............#.#.....#.. +........#.#......#..#........## +.............#...#.#.........## +...###...#..........##.......#. +.#..........#...##..#.#.....#.. +##...#.........#............... +......#....#....#.....#.....#.. +..........#....#...#...#..#...# +...##....#.#.#..#...##......... +#......#.#...##.###...#....#... +##.......##.#......##..#...#... +......#.............#.##.....## +#.......###....####.#...##....# +..#...#..#.......#..........#.. +#.....#..#..#..#.##...###...#.. +.....##.#..#..#..#.#....#...#.. +..#...#..................##.... +....#.#........##.............. +#...#.......##...#...#.#....... +..#...#........##....#.#....... +..........###...###...#......#. +#.....#..###...##...##..#..#..# +..#.....##.....#.......##..#.#. +........#........#.........#... +.................#....#.......# +.......#...#.....#...#.#....... +....##...............#...##...# +.##...#................#...#... +.............#................. +.#..#....#....#.#....#......... +.#.#..#..........#.......#..... +.....##.....##...#..#.......... +#...#.#.........#......#..#.... +........#....#...#....#.#.##... +....#..#........#...#...#...... +.#..#.....#.#...#.........#.... +.#..#..#...........#..#....#... +....###.............#..#....... +#......#..#..##..........#.#... +#..#..#.##..#...#.#.#.......... +....###......#.##.....#....#... +.##..#...#......##.#........... +..#..#.......#.....#.##....#.#. +.......#.#.#........#....##.... +..##...#....#...............### +#..##..#...........#.#....##... +...##..#.....................#. +###......#....#....###..#...##. +.........##............#..#...# +..#..........#...#.#.#......#.# +.......#.....##..##......#.##.. +#..........#.....##.#.......... +#.......#.#...#...#....#....... +#...#.....##.......#.#..#.#.#.. +.........#.#.#..#..#...#.###... +.................##...#....#... +###.......#..........##...#.... +#.#..#.........#....##.#....... +......#.#.....#........#....... +.......#.#........#......#.#..# +..............#..#...##....#..# +#...........#...##.....#..#.#.. +..#....#..#.#.#...#..#....#.#.. +...##.#.....#..#...##..#.....#. +..#.#................#........# +......#...#.............#...... +.##............#....#...#..#... +....#...#...........#.......#.. +.###..#.......#.............#.# +.#.#....#.#...........#.#...... +...#.........#.........#..#.... +...#..........#.#.....#.#...... +.....#........#....##......#... +..#.#.#......#..#.#......#....# +.#.#..#................#.#..... +.#.#.........##...#.......#.#.# +#..#.....#...#..#...........#.. +..##......####......#..#....### +#.....###....#.#........#..#..# +..##.#...#.#..##..........#..#. +#.........#.#.............#...# +...#.#...#...#.#.#....##....... +##.##...#.....#...#...........# +....#........#.#.....#......... +.................##..#..##...## +.....##....#...#...#.....#..#.. +....#...#........#............# +..#...........##....#...#...##. +.....#......#.........#..##.#.. \ No newline at end of file diff --git a/2020/day-03/solution.js b/2020/day-03/solution.js new file mode 100644 index 0000000..6d13efb --- /dev/null +++ b/2020/day-03/solution.js @@ -0,0 +1,35 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') +const { linesToArray } = require('../../2018/inputParser') +const { countTreesOnRoute } = require('./airportRoute') + +fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { + if (err) throw err + + initData = linesToArray(initData.trim()) + + const resetInput = () => { + // Deep copy to ensure we aren't mutating the original data + return JSON.parse(JSON.stringify(initData)) + } + + const part1 = () => { + const data = resetInput() + return countTreesOnRoute({ map: { rows: data } }) + } + + const part2 = () => { + const data = resetInput() + console.debug(data) + return 'No answer yet' + } + const answers = [] + answers.push(part1()) + answers.push(part2()) + + answers.forEach((ans, idx) => { + console.info(`-- Part ${idx + 1} --`) + console.info(`Answer: ${ans}`) + }) +}) From dfbfec2c7489cf1a84d8a6addf47595b2898bf03 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Sun, 6 Dec 2020 21:23:35 -0800 Subject: [PATCH 3/3] feat[2020-day-03]: calculate tree collisions for multiple slopes Solution for part 2 --- 2020/day-03/solution.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/2020/day-03/solution.js b/2020/day-03/solution.js index 6d13efb..f915c14 100644 --- a/2020/day-03/solution.js +++ b/2020/day-03/solution.js @@ -21,8 +21,20 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { const part2 = () => { const data = resetInput() - console.debug(data) - return 'No answer yet' + const slopes = [ + [1, 1], + [3, 1], // Same as default + [5, 1], + [7, 1], + [1, 2] + ] + // Multiple the results of multiple slopes + return slopes.reduce((itr, slope) => { + return itr * countTreesOnRoute({ + map: { rows: data }, + slope + }) + }, 1) } const answers = [] answers.push(part1()) 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