-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
43 lines (37 loc) · 1006 Bytes
/
part1.ts
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
import * as fs from 'fs';
const input = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((x) => x.split('').map((y) => ({ height: +y, visible: false })));
const calcVisibility = (tree, max) => {
if (tree.height > max) {
tree.visible = true;
max = tree.height;
}
return max;
};
for (let i = 0; i < input.length; i++) {
// each row
for (let j = 0, max = -1; j < input[i].length; j++) {
// left to right
max = calcVisibility(input[i][j], max);
}
for (let j = input[i].length - 1, max = -1; j >= 0; j--) {
// right to left
max = calcVisibility(input[i][j], max);
}
}
for (let j = 0; j < input[0].length; j++) {
// each column
for (let i = 0, max = -1; i < input.length; i++) {
// top to bottom
max = calcVisibility(input[i][j], max);
}
for (let i = input.length - 1, max = -1; i >= 0; i--) {
// bottom to top
max = calcVisibility(input[i][j], max);
}
}
let result = 0;
input.forEach((line) => (result += line.filter((x) => x.visible).length));
console.log(result);