AdventOfCode2020/day_3/task_1.js
2020-12-03 23:09:19 +01:00

17 lines
587 B
JavaScript

const fs = require('fs');
const file = fs.readFileSync('./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.`);