AdventOfCode2020/day_3/task_2.js
2020-12-05 11:35:52 +01:00

33 lines
1,004 B
JavaScript

const fs = require('fs');
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
const arr = file.split('\n');
const speeds = [
[1, 1],
[3, 1],
[5, 1],
[7, 1],
[1, 2]
]
let results = [];
for (const [ rightSpeed, downSpeed ] of speeds) {
let trees = 0;
let row = 0;
let pos = 1;
console.log(`Down ${downSpeed}, right ${rightSpeed}:`);
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 += downSpeed;
pos += rightSpeed;
if (pos > currentRow.length) pos -= currentRow.length;
}
console.log(`Encountered ${trees} trees.\n`);
results.push(trees);
}
console.log(`Result: ${results.join(' * ')} = ${results.reduce((acc, cur) => acc * cur)}`);