-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc3.js
34 lines (31 loc) · 907 Bytes
/
aoc3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require('fs').readFile('./aoc3.txt', 'utf8', (_, content) => {
const data = content.split('\n');
main(data);
main2(data);
});
function getTreeEncounteredCount(right, down, data) {
const width = data[0].length;
let curElt = '.';
let lineCursor = 0;
let treeEncounteredCount = 0;
for (let i = down; i < data.length; i += down) {
lineCursor += right;
curElt = data[i][lineCursor % width];
if(curElt === '#') {
treeEncounteredCount++;
}
}
return treeEncounteredCount;
}
function main(data) {
console.log(getTreeEncounteredCount(3, 1, data));
}
function main2(data) {
console.log(
getTreeEncounteredCount(1, 1, data)
* getTreeEncounteredCount(3, 1, data)
* getTreeEncounteredCount(5, 1, data)
* getTreeEncounteredCount(7, 1, data)
* getTreeEncounteredCount(1, 2, data)
);
}