advent-of-code/2020/day3/day3.js

35 lines
807 B
JavaScript
Raw Normal View History

2020-12-05 01:43:42 +01:00
#!/bin/node
const fs = require('fs');
fs.readFile('input.txt', (err, data) => {
const lines = data.toString().split('\n').filter(l => l.length > 0);
width = lines[0].length;
const slopes = [
[1, 1],
[3, 1],
[5, 1],
[7, 1],
[1, 2],
];
const positions = slopes.map(_ => 0);
const results = slopes.map(_ => 0);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < slopes.length; j++) {
const [right, down] = slopes[j];
if (i % down === 0) {
if (line[positions[j]] === '#') results[j]++;
positions[j] = (positions[j] + right) % width;
}
}
}
console.log(results, results.reduce((a, n) => a * n, 1));
});