Untitled

 avatar
unknown
plain_text
11 days ago
1.3 kB
7
Indexable
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'hourglassSum' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY arr as parameter.
#

def hourglassSum(arr):
    max_sum = -float('inf')  # Initialize with the smallest possible value

    for i in range(4):  # We only loop till row 3, since hourglass needs 3 rows
        for j in range(4):  # We only loop till column 3, since hourglass needs 3 columns
            # Calculate the sum of the current hourglass
            hourglass = (arr[i][j] + arr[i][j+1] + arr[i][j+2] +   # Top row
                         arr[i+1][j+1] +                         # Middle row (only middle element)
                         arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2])  # Bottom row

            # Update max_sum if the current hourglass sum is greater
            max_sum = max(max_sum, hourglass)

    return max_sum

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr = []

    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))

    result = hourglassSum(arr)

    fptr.write(str(result) + '\n')

    fptr.close()
Editor is loading...
Leave a Comment