-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge40.cpp
84 lines (75 loc) · 2.05 KB
/
challenge40.cpp
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
// AquaQ Challenge Hub
// Challenge 40: Prominence promenade
// https://challenges.aquaq.co.uk/challenge/40
#include <array>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <gsl/util>
static bool readFile(const std::string& fileName, std::vector<std::string>& lines)
{
std::ifstream in{fileName};
if (!in) {
std::cerr << "Cannot open file " << fileName << std::endl;
return false;
}
auto closeStream = gsl::finally([&in] { in.close(); });
std::string str;
while (std::getline(in, str)) {
lines.push_back(str);
}
return true;
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
std::vector<uint16_t> values{0};
{
std::istringstream iss{lines[0]};
uint16_t num;
while (iss >> num) {
values.push_back(num);
}
values.push_back(0);
}
std::vector<size_t> peaks;
uint16_t max{0};
for (size_t i = 1; i < values.size() - 1; ++i) {
if (values[i - 1] < values[i] && values[i + 1] < values[i]) {
peaks.push_back(i);
max = std::max(max, values[i]);
}
}
uint16_t sumPromincence{0};
for (size_t i = 0; i < peaks.size(); ++i) {
const auto val = values[peaks[i]];
if (val == max) {
sumPromincence += max;
continue;
}
uint16_t minL{max};
for (size_t j = peaks[i] - 1; j > 0; --j) {
if (values[j] >= val) {
break;
}
minL = std::min(minL, values[j]);
}
uint16_t minR{max};
for (size_t j = peaks[i] + 1; j < values.size() - 1; ++j) {
if (values[j] >= val) {
break;
}
minR = std::min(minR, values[j]);
}
sumPromincence += std::min(val - minL, val - minR);
}
std::cout << sumPromincence << std::endl;
return EXIT_SUCCESS;
}