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.
26 lines
636 B
JavaScript
26 lines
636 B
JavaScript
4 years ago
|
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);
|