Skip to content

Commit 1aa63ed

Browse files
committed
Add missing solution
1 parent 0d1a7fa commit 1aa63ed

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function solve(args) {
2+
"use strict";
3+
4+
let heights = args[0].split(" ")
5+
.map(Number);
6+
7+
let sum = 0;
8+
9+
for (let i = 2; i < heights.length - 2; i += 1) {
10+
if (isPocket(i, heights)) {
11+
sum += heights[i];
12+
}
13+
}
14+
15+
console.log(sum);
16+
17+
// return sum;
18+
19+
function isPocket(index, heights) {
20+
return isValley(index, heights) && isPeak(index - 1, heights) && isPeak(index + 1, heights);
21+
}
22+
23+
function isValley(index, valley) {
24+
return valley[index] < valley[index - 1] &&
25+
valley[index] < valley[index + 1];
26+
}
27+
28+
function isPeak(index, valley) {
29+
return valley[index] > valley[index - 1] &&
30+
valley[index] > valley[index + 1];
31+
}
32+
}

0 commit comments

Comments
 (0)