-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheight.js
More file actions
133 lines (119 loc) · 2.73 KB
/
Copy patheight.js
File metadata and controls
133 lines (119 loc) · 2.73 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { readFile } = require('fs/promises');
let TREES = [];
let VIS = [];
let SCORE = [];
// 8
async function init() {
const input = await readFile('eight.txt', 'utf8');
// const input = await readFile('eight.test.txt', 'utf8');
const rows = input.split('\n');
TREES = rows.map((row, i) => {
VIS[i] = [];
SCORE[i] = [];
return row.split('').map((d, j) => {
VIS[i][j] = false;
SCORE[i][j] = false;
return parseInt(d);
});
});
// console.log(TREES);
// check from left
for (let i = 0; i < TREES.length; i++) {
let highest = -1;
for (let j = 0; j < TREES[i].length; j++) {
if (TREES[i][j] > highest) {
VIS[i][j] = true;
highest = TREES[i][j];
}
}
}
// check from right
for (let i = 0; i < TREES.length; i++) {
let highest = -1;
for (let j = TREES[i].length - 1; j >= 0; j--) {
if (TREES[i][j] > highest) {
VIS[i][j] = true;
highest = TREES[i][j];
}
}
}
// check from top
for (let j = 0; j < TREES[0].length; j++) {
let highest = -1;
for (let i = 0; i < TREES.length; i++) {
if (TREES[i][j] > highest) {
VIS[i][j] = true;
highest = TREES[i][j];
}
}
}
// check from bottom
for (let j = 0; j < TREES[0].length; j++) {
let highest = -1;
for (let i = TREES.length - 1; i >= 0; i--) {
if (TREES[i][j] > highest) {
VIS[i][j] = true;
highest = TREES[i][j];
}
}
}
let sum = 0;
for (let i = 0; i < VIS.length; i++) {
for (let j = 0; j < VIS[i].length; j++) {
if (VIS[i][j]) {
sum++;
}
}
}
// check for best spot
for (let i = 0; i < TREES.length; i++) {
for (let j = 0; j < TREES[i].length; j++) {
const height = TREES[i][j];
const score = [0, 0, 0, 0];
// right
for (let y = j + 1; y < TREES[0].length; y++) {
score[0]++;
if (TREES[i][y] >= height) {
break;
}
}
// left
for (let y = j - 1; y >= 0; y--) {
score[1]++;
if (TREES[i][y] >= height) {
break;
}
}
// up
for (let x = i + 1; x < TREES.length; x++) {
score[2]++;
if (TREES[x][j] >= height) {
break;
}
}
// dowm
for (let x = i - 1; x >= 0; x--) {
score[3]++;
if (TREES[x][j] >= height) {
break;
}
}
SCORE[i][j] = score[0] * score[1] * score[2] * score[3];
}
}
const max = SCORE.reduce((a, row) => {
r = row.reduce((acc, d) => {
if (d > acc) {
return d;
}
return acc;
});
if (r > a) {
return r;
}
return a;
}, 0);
console.log({ max });
// console.log({ SCORE });
}
init();