https://projecteuler.net/problem=11
Any thoughts? Also, when measuring with chrono it takes supposedly 4 ms to compute, is this reliable value? Previous problems took little longer to compute so I'm suspicious, and I'm still a beginner so I'm not sure how long it "should" take or if chrono is good for measuring computing time.
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::ifstream input("grid.txt");
std::vector<std::vector<int>> numbers(20, std::vector<int>(20));
char c;
std::string str;
int row{ 0 };
int column{ 0 };
//convert from ascii to decimal
while (input.get(c))
{
str += c;
if (((int)c - 48 == -16) || ((int)c - 48 == -38)) //space or new line
{
numbers[row][column] = std::stoi(str);
column++;
str.clear();
}
if (column == 20)
{
row++;
column = 0;
}
}
int product;
int maxProduct{ 0 };
for (int i = 0; i < 20; i++)
{
product = 0;
for (int j = 0; j < 20; j++)
{
// horizontal
if (j >= 3)
{
product = numbers[i][j] * numbers[i][j - 1] * numbers[i][j - 2] * numbers[i][j - 3];
if (product > maxProduct)
maxProduct = product;
}
if (j <= 16)
{
product = numbers[i][j] * numbers[i][j + 1] * numbers[i][j + 2] * numbers[i][j + 3];
if (product > maxProduct)
maxProduct = product;
}
// vertical
if (i >= 3)
{
product = numbers[i][j] * numbers[i - 1][j] * numbers[i - 2][j] * numbers[i - 3][j];
if (product > maxProduct)
maxProduct = product;
}
if (i <= 16)
{
product = numbers[i][j] * numbers[i + 1][j] * numbers[i + 2][j] * numbers[i + 3][j];
if (product > maxProduct)
maxProduct = product;
}
// diagonal
if (i >= 3 && j >= 3)
{
product = numbers[i][j] * numbers[i - 1][j - 1] * numbers[i - 2][j - 2] * numbers[i - 3][j - 3];
if (product > maxProduct)
maxProduct = product;
}
if (i <= 16 && j <= 16)
{
product = numbers[i][j] * numbers[i + 1][j + 1] * numbers[i + 2][j + 2] * numbers[i + 3][j + 3];
if (product > maxProduct)
maxProduct = product;
}
if (i >= 3 && j <= 16)
{
product = numbers[i][j] * numbers[i - 1][j + 1] * numbers[i - 2][j + 2] * numbers[i - 3][j + 3];
if (product > maxProduct)
maxProduct = product;
}
if (i <= 16 && j >= 3)
{
product = numbers[i][j] * numbers[i + 1][j - 1] * numbers[i + 2][j - 2] * numbers[i + 3][j - 3];
if (product > maxProduct)
maxProduct = product;
}
}
}
return 0;
}
maxProduct
at the end? How do you find out the answer, run it under the debugger and examinemaxProduct
beforemain
returns? \$\endgroup\$