blob: 258b27b1a7d694230e88b1242f4bef2c22261990 (
plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
|
import fs from "fs";
function range(first: number, second?: number) {
const start = second ? first : 0;
const end = second ? second : first;
return new Array(end - start)
.fill("fuck you js")
.map((_wtf, index) => index + start);
}
const input = fs.readFileSync("input.txt").toString();
const array = input
.split("\n")
.flatMap((line) => line.split(""))
.map(Number);
console.log(array);
const N = Math.sqrt(array.length);
console.assert(Math.round(N) === N, "Matrix should be square");
const visibilitySet = new Set<number>();
function lookup(startIndex: number, step: number) {
range(N).reduce((acc, stepIndex) => {
const currentIndex = startIndex + stepIndex * step;
const item = array[currentIndex];
if (item > acc) {
visibilitySet.add(currentIndex);
return item;
}
return acc;
}, -1);
}
const edgeSize = N - 2;
// Down
range(1, 1 + edgeSize).map((index) => lookup(index, N));
// Up
range(1 + (N - 1) * N, 1 + edgeSize + (N - 1) * N).map((index) =>
lookup(index, -N),
);
// Right
range(edgeSize)
.map((v) => N * (v + 1))
.map((index) => lookup(index, 1));
// Left
range(edgeSize)
.map((v) => N * (v + 1) + N - 1)
.map((index) => lookup(index, -1));
console.log(visibilitySet);
console.log(Array.from(visibilitySet.values()).length + 4);
|