My solution, updated in response to Jamal and vnp:
#include <fstream>
#include <iostream>
using namespace std;
const size_t DIGITS_IN_PRODUCT = 13;
void refill_window(string &series, size_t &i, uint8_t* window);
uint64_t window_product(uint8_t* window);
int main() {
// read series.txt into a string without newline characters
ifstream ifs("../series.txt");
string series, line;
while (getline(ifs, line)) {
series += line;
}
ifs.close();
if (series.size() < DIGITS_IN_PRODUCT) {
cout << "Error: series.txt contains too few elements." << endl;
exit(2);
}
uint8_t window[DIGITS_IN_PRODUCT];
for (size_t i = 0; (i < DIGITS_IN_PRODUCT) && (i < series.length());
i++) {
if (series[i] == '0') {
refill_window(series, i, window);
break;
}
window[i] = series[i] - '0';
}
uint64_t product = window_product(window);
uint64_t max_product = product;
for (size_t i = DIGITS_IN_PRODUCT; i < series.length(); i++) {
if (series[i] == '0') {
refill_window(series, i, window);
product = window_product(window);
} else {
product /= window[i % DIGITS_IN_PRODUCT];
window[i % DIGITS_IN_PRODUCT] = series[i] - '0';
product *= window[i % DIGITS_IN_PRODUCT];
}
if (product > max_product) {
max_product = product;
}
}
cout << max_product << endl;
}
void refill_window(string &series, size_t &i, uint8_t* window) {
i++;
for (size_t j = 0;
(j < DIGITS_IN_PRODUCT) && (i < series.length()); j++, i++) {
if (series[i] == '0') {
refill_window(series, i, window);
return;
}
window[i % DIGITS_IN_PRODUCT] = series[i] - '0';
}
i--; // correct for final increment in for loop.
}
uint64_t window_product(uint8_t* window) {
uint64_t product = 1;
for (size_t i = 0; i < DIGITS_IN_PRODUCT; i++) {
product *= window[i];
}
return product;
}