17 lines
598 B
JavaScript
17 lines
598 B
JavaScript
const fs = require('fs');
|
|
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
|
|
const arr = file.split('\n');
|
|
|
|
let trees = 0;
|
|
let row = 0;
|
|
let pos = 1;
|
|
|
|
while (row < arr.length) {
|
|
const currentRow = arr[row];
|
|
// console.log(`${currentRow.slice(0, pos - 1)}${currentRow.charAt(pos - 1) == '#' ? 'X' : 'O'}${currentRow.slice(pos - 1)}` + ' ' + pos + ' => ' + currentRow.charAt(pos - 1));
|
|
if (currentRow.charAt(pos - 1) == '#') trees++;
|
|
row += 1;
|
|
pos += 3;
|
|
if (pos > currentRow.length) pos -= currentRow.length;
|
|
}
|
|
console.log(`Encountered ${trees} trees.`); |