-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday03.js
46 lines (33 loc) · 831 Bytes
/
day03.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
35
36
37
38
39
40
41
42
43
44
45
46
const fs = require('fs');
const lines = fs.readFileSync('day03.txt', {encoding: 'utf-8'}).split('\n').filter(x => x);
class Map {
constructor(map) {
this.map = map;
}
getPosition(x, y) {
return this.map[y][x%this.map[0].length];
}
getHeight() {
return this.map.length;
}
}
const map = new Map(lines.map(line => [...line]));
function trySlope(dx, dy) {
let x = 0;
let y = 0;
let trees = 0;
while(y < map.getHeight()) {
const current = map.getPosition(x, y);
if(current == '#') trees++;
x += dx;
y += dy;
}
return trees;
}
console.log(trySlope(3, 1));
const slopes = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]];
let result = 1;
for (const slope of slopes) {
result *= trySlope(...slope);
}
console.log(result);