MADE AN ERROR
#include <stdio.h>
#include <stdlib.h>
static unsigned heights[] =
{
5, 5, 5, 0, 2, 0, 3, 1, 0, 1, 3, 2, 1
};
int main()
{
size_t len = sizeof(heights) / sizeof(unsigned);
int trend = 0;
int* indices = malloc(len * sizeof(int));
size_t indices_len = 0;
// find valley peaks
for (unsigned i = 1; i < len; i++)
{
if (heights[i-1] < heights[i])
trend = 1;
else if (heights[i-1] > heights[i] && trend >= 0)
{
indices[indices_len] = i - 1;
indices_len++;
trend = -1;
}
}
printf("These points make up the valley peaks.\n[");
for (unsigned i = 0; i < indices_len; i++)
{
printf("%d", indices[i]);
if (i < indices_len - 1) printf(", ");
}
printf("]\n");
// For every pair of adjacent peaks, the water height and the height must add to a constant. This constant is the smaller of the two peaks.
unsigned sum = 0;
for (unsigned i = 1; i < indices_len; i++)
{
size_t idx1 = indices[i-1];
size_t idx2 = indices[i];
unsigned v1 = heights[idx1];
unsigned v2 = heights[idx2];
unsigned v = v1 < v2 ? v1 : v2;
for (unsigned j = idx1 + 1; j < idx2; j++)
sum += v - heights[j];
}
free(indices);
printf("The sum is %d.", sum);
return 0;
}
>>102533279
>But atleast it's better than wasting time gaming or gooning to cartoons.
Baby steps.