Project Euler #11 - Largest product in a grid C++ 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 &lt;fstream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

int main()
{
    std::ifstream input("grid.txt");
    std::vector&lt;std::vector&lt;int&gt;&gt; numbers(20, std::vector&lt;int&gt;(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 &lt; 20; i++)
    {
        product = 0;
        for (int j = 0; j &lt; 20; j++)
        {
            // horizontal
            if (j &gt;= 3)
            {
                product = numbers[i][j] * numbers[i][j - 1] * numbers[i][j - 2] * numbers[i][j - 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }

            if (j &lt;= 16)
            {
                product = numbers[i][j] * numbers[i][j + 1] * numbers[i][j + 2] * numbers[i][j + 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
            // vertical
            if (i &gt;= 3)
            {
                product = numbers[i][j] * numbers[i - 1][j] * numbers[i - 2][j] * numbers[i - 3][j];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }

            if (i &lt;= 16)
            {
                product = numbers[i][j] * numbers[i + 1][j] * numbers[i + 2][j] * numbers[i + 3][j];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
            // diagonal
            if (i &gt;= 3 &amp;&amp; j &gt;= 3)
            {
                product = numbers[i][j] * numbers[i - 1][j - 1] * numbers[i - 2][j - 2] * numbers[i - 3][j - 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
            if (i &lt;= 16 &amp;&amp; j &lt;= 16)
            {
                product = numbers[i][j] * numbers[i + 1][j + 1] * numbers[i + 2][j + 2] * numbers[i + 3][j + 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
            if (i &gt;= 3 &amp;&amp; j &lt;= 16)
            {
                product = numbers[i][j] * numbers[i - 1][j + 1] * numbers[i - 2][j + 2] * numbers[i - 3][j + 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
            if (i &lt;= 16 &amp;&amp; j &gt;= 3)
            {
                product = numbers[i][j] * numbers[i + 1][j - 1] * numbers[i + 2][j - 2] * numbers[i + 3][j - 3];
                if (product &gt; maxProduct)
                    maxProduct = product;
            }
        }
    }

    return 0;
}
Input

Using magic numbers like 48 and -16 makes your code very cryptic and hard to understand. Consider comparing with the characters instead.

Algorithm / Logic

I think two things that could really simplify your code are:


Limit the loops iterations from [0, 0] to [16, 16]. This will make you avoid some unnecessary checks.
Only try to get the 4 numbers in the forward direction. There is no need to check in the backward direction as well since it's already checked in the forward direction.