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);