Last active
December 8, 2020 21:24
-
-
Save cranderveldt/f1b87222f07dd461f1e63ce7aa143f4a to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puzzleInput = `` | |
// Part 1 | |
const parseInput = (input) => { | |
return input.split('\n') | |
} | |
const traverse = (rows, colInc, rowInc = 1) => { | |
return rows.reduce((acc, row, index) => { | |
if (index % rowInc !== 0) { return acc } | |
const position = (index / rowInc) * colInc % row.length | |
return acc + +(row[position] === '#') | |
}, 0) | |
} | |
const partOne = (input) => { | |
const rows = parseInput(input) | |
const treeCount = traverse(rows, 3) | |
console.log(treeCount) | |
} | |
partOne(puzzleInput) | |
// Part 2 | |
const partTwo = (input) => { | |
const rows = parseInput(input) | |
const slopes = [ | |
[1, 1], | |
[3, 1], | |
[5, 1], | |
[7, 1], | |
[1, 2], | |
] | |
const treeCount = slopes.reduce((acc, [colInc, rowInc]) => acc * traverse(rows, colInc, rowInc), 1) | |
console.log(treeCount) | |
} | |
partTwo(puzzleInput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment