-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (63 loc) · 1.87 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const input = require("../../utils/getInput")(__dirname, { split: "\n" }).map((l) => l.split("").map((n) => parseInt(n, 10)));
const findHigher = (input, [x, y], highest, set) => {
const n = input[x][y];
if (n > highest) {
highest = n;
set.add(`${x}_${y}`);
}
return [highest, set];
};
const visibleTrees = (input) => {
let cache = new Set();
for (let x in input) {
for (let highest = -1, y = 0; y < input[x].length; y++) {
[highest, cache] = findHigher(input, [x, y], highest, cache);
}
for (let highest = -1, y = input[x].length - 1; y >= 0; y--) {
[highest, cache] = findHigher(input, [x, y], highest, cache);
}
}
for (let y in input[0]) {
for (let highest = -1, x = 0; x < input.length; x++) {
[highest, cache] = findHigher(input, [x, y], highest, cache);
}
for (let highest = -1, x = input.length - 1; x >= 0; x--) {
[highest, cache] = findHigher(input, [x, y], highest, cache);
}
}
return cache.size;
};
console.log(visibleTrees(input));
const bestArea = (input) => {
const results = [];
for (let x = 0; x < input.length; x++) {
for (let y = 0; y < input[x].length; y++) {
let tot = {
l: 0,
r: 0,
t: 0,
b: 0
};
const shouldStop = (xL, yL) => input[xL][yL] >= input[x][y];
for (let xL = x - 1; xL >= 0; xL--) {
tot.t++;
if (shouldStop(xL, y)) break;
}
for (let xL = x + 1; xL < input.length; xL++) {
tot.b++;
if (shouldStop(xL, y)) break;
}
for (let yL = y - 1; yL >= 0; yL--) {
tot.l++;
if (shouldStop(x, yL)) break;
}
for (let yL = y + 1; yL < input[x].length; yL++) {
tot.r++;
if (shouldStop(x, yL)) break;
}
results.push(tot.l * tot.t * tot.r * tot.b);
}
}
return Math.max(...results);
};
console.log(bestArea(input));