const fs = require("fs"); function setCharAt(str, index, chr) { if (index > str.length - 1) return str; return str.substring(0, index) + chr + str.substring(index + 1); } const input = fs.readFileSync("input", "utf-8"); const rows = input.split("\n"); console.log(rows.slice(0, 10)); let countTrees = 0; let curX = 0; for (let curY = 1; curY < rows.length; curY++) { curX = (curX + 3) % rows[curY].length; const row = rows[curY]; const field = rows[curY][curX]; rows[curY] = setCharAt(row, curX, field === "#" ? "X" : "O"); if (field === "#") countTrees++; } console.log(rows.slice(0, 10)); console.log(countTrees);