-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge38.cpp
74 lines (68 loc) · 1.95 KB
/
challenge38.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
// AquaQ Challenge Hub
// Challenge 38: Number Neighbours
// https://challenges.aquaq.co.uk/challenge/38
#include <fstream>
#include <iostream>
#include <numeric>
#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;
}
}
uint64_t sum{};
for (const auto& line : lines) {
std::vector<uint32_t> numbers;
{
std::istringstream iss{line};
uint32_t n;
while (iss >> n) {
numbers.push_back(n);
}
}
const auto len = numbers.size();
sum += len;
for (size_t i = 0; i < len; ++i) {
// For each number
for (size_t j = 1; j < len; ++j) {
// For each streak size
const size_t start = j < i ? i - j : 0;
const size_t end = i + j + 1 > len ? len - j : i + 1;
bool found{false};
for (size_t k = start; k < end; ++k) {
if (const auto s = std::accumulate(numbers.cbegin() + k, numbers.cbegin() + k + j + 1, size_t{0});
s % (j + 1) == 0) {
sum++;
found = true;
break;
}
}
if (!found) {
break;
}
}
}
}
std::cout << sum << std::endl;
return EXIT_SUCCESS;
}