-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge13.cpp
57 lines (51 loc) · 1.64 KB
/
challenge13.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
// AquaQ Challenge Hub
// Challenge 13: O RLE?
// https://challenges.aquaq.co.uk/challenge/13
#include <algorithm>
#include <fstream>
#include <iostream>
#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;
}
}
size_t sum{};
for (const auto& line : lines) {
size_t max{0};
for (size_t len = 1; len < line.size(); ++len) {
// Forward check - ignoring trailing extra characters, e.g., ABABABCD
if (const auto it = std::mismatch(line.begin(), line.end(), line.begin() + len, line.end());
it.second != line.begin() + len) {
max = std::max(max, (it.second - line.begin()) / len);
}
// Reverse check - ignoring leading extra characters, e.g., CDABABAB
if (const auto it = std::mismatch(line.rbegin(), line.rend(), line.rbegin() + len, line.rend());
it.second != line.rbegin() + len) {
max = std::max(max, (it.second - line.rbegin()) / len);
}
}
sum += max;
}
std::cout << sum << std::endl;
return EXIT_SUCCESS;
}