Last active
December 3, 2020 05:32
-
-
Save gacardinal/3c5cd4d9918b392a1ac04600334e798b to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 3 TypeScript
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
import { readFileSync } from 'fs'; | |
const map = readFileSync('input.txt').toString('utf8').split('\n'); | |
const patternWidth = map[0].length; | |
const countTreesInSlope = (right: number, down: number) => { | |
let totalTrees = 0; | |
let y= 0; | |
for (let x = 0; x < map.length; x = x + down) { | |
const line = map[x]; | |
if (line.charAt(y) === '#') { | |
totalTrees = totalTrees + 1; | |
} | |
if (y + right < patternWidth) { | |
y = y + right; | |
} else { | |
y = y + right - patternWidth; | |
} | |
} | |
return totalTrees; | |
} | |
console.log(`Chapter one: ${countTreesInSlope(3, 1)}`); | |
let slopes = [ | |
countTreesInSlope(1, 1), | |
countTreesInSlope(3, 1), | |
countTreesInSlope(5, 1), | |
countTreesInSlope(7, 1), | |
countTreesInSlope(1, 2) | |
]; | |
console.log(`Chapter two: ${slopes.reduce((a, b) => a * b)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment