Day 11 - 2D Array

November 14, 2017


Context

Given a 2D Array, :

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

There are hourglasses in , and an hourglass sum is the sum of an hourglass’ values.

Task

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

Input Format

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

Constraints

Output Format

Print the largest (maximum) hourglass sum found in .

Sample Input

Sample Output

19

Explanation

contains the following hourglasses:

The hourglass with the maximum sum () is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys

arr = []
sums = []
for arr_i in range(6):
   arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
   arr.append(arr_t)

for i in range(4):
    for j in range(4):
        sum = 0
        for k in range(3):
            sum += arr[i][j + k]
            sum += arr[i+2][j + k]
        sum += arr[i+1][j+1]    
        sums.append(sum)
print(max(sums))