Add '2020/' from commit 'aaabfa90c9033044d0a9d5fe6776b718711ef46c'
git-subtree-dir: 2020 git-subtree-mainline:ab8f135946
git-subtree-split:aaabfa90c9
This commit is contained in:
57
2020/03/part2.js
Normal file
57
2020/03/part2.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const fs = require('fs')
|
||||
const readline = require('readline');
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: fs.createReadStream('input'),
|
||||
console: false
|
||||
});
|
||||
|
||||
|
||||
// array of arrays of bool. Coordinates are [y][x]. True = tree.
|
||||
let map = [];
|
||||
|
||||
readInterface.on('line', function(line) {
|
||||
let row = [];
|
||||
|
||||
line.split('').forEach(function(char) {
|
||||
if (char == '.') {
|
||||
row.push(false);
|
||||
} else if (char == '#') {
|
||||
row.push(true);
|
||||
}; // ignore any other character
|
||||
});
|
||||
|
||||
map.push(row);
|
||||
});
|
||||
|
||||
|
||||
readInterface.on('close', function(line) {
|
||||
let totals = [];
|
||||
|
||||
[[1,1], [3,1], [5,1], [7,1], [1,2]].forEach(function(step) {
|
||||
|
||||
let treesHit = 0;
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
|
||||
while (y + 1 < map.length) {
|
||||
x += step[0];
|
||||
y += step[1];
|
||||
|
||||
if (x + 1 > map[y].length) {
|
||||
x -= map[y].length;
|
||||
}
|
||||
|
||||
if (map[y][x]) {
|
||||
treesHit++;
|
||||
}
|
||||
|
||||
// console.log(x, y, map[y][x]);
|
||||
}
|
||||
|
||||
totals.push(treesHit);
|
||||
});
|
||||
|
||||
console.log(totals.reduce((a,b) => a * b));
|
||||
});
|
||||
|
Reference in New Issue
Block a user