Skip to content

Instantly share code, notes, and snippets.

@gacardinal
Last active December 3, 2020 05:32
Show Gist options
  • Save gacardinal/3c5cd4d9918b392a1ac04600334e798b to your computer and use it in GitHub Desktop.
Save gacardinal/3c5cd4d9918b392a1ac04600334e798b to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day 3 TypeScript
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