You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
769 B
JavaScript

4 years ago
const fs = require("fs");
const input = fs.readFileSync("input", "utf-8").trim();
const rows = input.split("\n");
console.log("Num of rows:", rows.length);
console.log("Column width:", rows[0].length);
function run(dX, dY) {
let countTrees = 0;
let curX = 0;
for (let curY = dY; curY < rows.length; curY += dY) {
curX = (curX + dX) % rows[curY].length;
const field = rows[curY][curX];
if (field === "#") countTrees++;
}
return countTrees;
}
const runs = [
[1, 1],
[3, 1],
[5, 1],
[7, 1],
[1, 2],
];
let solution = 1;
for (const args of runs) {
const countTrees = run(...args);
console.log(`Right: ${args[0]}, down: ${args[1]}: Trees: ${countTrees}`);
solution *= countTrees;
}
console.log("Solution:");
console.log(solution);