-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
60 lines (48 loc) · 1.04 KB
/
main.c
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
typedef int bool;
#define true 1
#define false 0
#include <stdlib.h>
#include <stdio.h>
bool validateInput(int argCount) {
if (argCount > 100) {
printf("Input greater than 100. Bailing.\n");
return false;
} else if (argCount == 0) {
printf("Input empty. Bailing.\n");
return false;
}
return true;
}
int main(int argc, char *argv[]) {
if (!validateInput(argc)) {
return 0;
}
int heightCount = argc - 1;
int heights[heightCount];
for (int i = 0; i < heightCount; i++) {
heights[i] = atoi(argv[i + 1]);
}
int cliff = 0;
int volume = 0;
bool counting = false;
for (int i = 0; i < heightCount; i++) {
if (i == 0) {
continue;
}
int previous = heights[i - 1];
int current = heights[i];
if (previous > current && !counting) {
counting = true;
cliff = previous;
}
if (counting) {
if (cliff > current && i != heightCount - 1) {
volume += cliff - current;
} else {
counting = false;
}
}
}
printf("%i", volume);
return 0;
}