Game development as an Art

Sato Game Dev

Code Exercises


2D Arrays


Source: Hacker Rank

Context

Given 6x6 2D Array, A:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

We define an hourglass in A to be a subset of values with indices falling in this pattern in A's graphical representation:

a b c
d
e f g

There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass' values.

Task

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

Input Format

There are 6 lines of input, where each line contains 6 space-separated integers describing 2D Array A; every value in A will be in the inclusive range of -9 to 9.
Constraints

-9 <= A[i][j] <= 9
0 <= i, j <= 5

Output Format

Print the largest (maximum) hourglass sum found in A.

Sample Input

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

Sample Output

19

Explanation

A contains the following hourglasses:


The hourglass with the maximum sum (19) is: 2 4 4
2
1 2 4


My solution:
C#:
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2DArrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[6][];

            for (int i = 0; i < 6; i++)
            {
                arr[i] = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp));
            }

            int maxSum = int.MinValue;
            int actualSum = int.MinValue;
            //reading throuth array
            for (int aux=0; aux< arr.Length - 2; aux++)
            {
                for(int aux2 = 0; aux2 < arr.Length - 2; aux2++)
                {
                    //reading H positions
                    actualSum = arr[aux][aux2] + arr[aux][aux2+1] + arr[aux][aux2+2] +
                        arr[aux + 1][aux2 + 1] +
                        arr[aux+2][aux2] + arr[aux + 2  ][aux2+1] + arr[aux + 2][aux2+2];

                    if (maxSum < actualSum)
                        maxSum = actualSum;
                }
            }

            Console.WriteLine(maxSum);
            Console.ReadKey();
        }
    }
}