forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
35 lines (30 loc) · 1.32 KB
/
Copy pathexample.js
File metadata and controls
35 lines (30 loc) · 1.32 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
export class Rectangles {
static count(diagram) {
const rows = diagram.length;
const cols = rows ? diagram[0].length : 0;
let rectangles = 0;
// All possible topleft corners
for (let y = 0; y < rows - 1; y += 1) {
for (let x = 0; x < cols - 1; x += 1) {
if (diagram[y].charAt(x) === '+') {
// All possible bottomright corners
for (let j = y + 1; j < rows; j += 1) {
for (let i = x + 1; i < cols; i += 1) {
// Check if all corners are valid
if (diagram[j].charAt(i) === '+' && diagram[y].charAt(i) === '+' && diagram[j].charAt(x) === '+') {
let validRectangle = true;
// Check if all sides are valid
for (let s = x + 1; s < i; s += 1) if (!'+-'.includes(diagram[y].charAt(s))) validRectangle = false;
for (let s = x + 1; s < i; s += 1) if (!'+-'.includes(diagram[j].charAt(s))) validRectangle = false;
for (let t = y + 1; t < j; t += 1) if (!'+|'.includes(diagram[t].charAt(x))) validRectangle = false;
for (let t = y + 1; t < j; t += 1) if (!'+|'.includes(diagram[t].charAt(i))) validRectangle = false;
if (validRectangle) rectangles += 1;
}
}
}
}
}
}
return rectangles;
}
}