-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsurface.js
100 lines (92 loc) · 3.14 KB
/
surface.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const arr = [[1]]
// const arr = [[1, 3, 4], [2, 2, 3], [1, 2, 4]]
function surface(A) {
let maxVal= 0;
let base = 0
for (let t=0; t<A.length; t++) {
if (Math.max(...A[t])>maxVal) {
maxVal = Math.max(...A[t]);
}
base += A[t].length
}
console.log(maxVal)
let area = 0;
function sum(r, a) {
return Array.isArray(a) ? a.reduce(sum, r) : r + a;
}
if (A[0].length !== null || A[0].length !== undefined || A[0].length >= 1){
// area = maxVal * A.length * A[0].length
area = maxVal * base * 4 + base * 2
// console.log(A.reduce(sum, 0))
// 4 + base * 2
// console.log(base)
// area = A.reduce(sum, 0) * 6
} else if (A[0].length === null || A[0].length === undefined || A[0].length < 1) {
return 0
}
console.log(area)
for (let k=1; k<maxVal+1; k++) {
let noArea = 0
console.log('round ' + k)
for (let i=0; i<A.length; i++) {
for (let j=0; j<A[i].length; j++){
console.log('Value '+A[i][j])
if (A[i][j]>=k){
if (i > 0 && i < A.length-1) {
if (A[i][j - 1] >=k ) {
console.log(1)
noArea--
}
if (A[i][j + 1] >=k ) {
console.log(2)
noArea--
}
if (A[i - 1][j] >=k ) {
console.log(3)
noArea--
}
if (A[i + 1][j] >=k ) {
console.log(4)
noArea--
}
} else if (i === 0) {
if (A[i][j - 1] >=k) {
console.log(5)
noArea--
}
if (A[i][j + 1] >=k) {
console.log(6)
noArea--
}
if (A[i + 1][j] >=k) {
console.log(7)
noArea--
}
} else if (i === A.length - 1) {
if (A[i][j - 1] >=k) {
console.log(8)
noArea--
}
if (A[i][j + 1] >=k) {
console.log(9)
noArea--
}
if (A[i - 1][j] >=k) {
console.log(11)
noArea--
}
}
} else if (A[i][j] < k) {
console.log(12)
noArea += -4;
}
console.log(noArea)
}
}
console.log('areas to remove '+noArea)
area = area + noArea
console.log('Current area ' + area)
}
return area
}
console.log(surface(arr))