jsadvent/solutions/2020/3.js
2020-12-10 02:33:00 -06:00

27 lines
630 B
JavaScript

const { Glib } = require('../../lib');
const parse = (input) => Glib.fromLines(input).array;
const TREE = '#';
const solve = (input, yIncrement, xMultiplier) => {
input = parse(input);
let xLoc = 0;
let treeCount = 0;
for (let yLoc = 0; yLoc < input.length; yLoc += yIncrement) {
if (input[yLoc][(yLoc * xMultiplier) % input[yLoc].length] === TREE) {
treeCount++;
}
}
return treeCount;
};
module.exports = {
'1': (input) => solve(input, 1, 3),
'2': (input) =>
[[1, 1], [1, 3], [1, 5], [1, 7], [2, 1]]
.map(([y, x]) => solve(input, y, x / y))
.reduce((a, b) => a * b),
parse,
};