Skip to content

Instantly share code, notes, and snippets.

@kevinah95
Created April 17, 2023 21:17
Show Gist options
  • Save kevinah95/6c1668b6a3afd68025b0e0b57f81dc2a to your computer and use it in GitHub Desktop.
Save kevinah95/6c1668b6a3afd68025b0e0b57f81dc2a to your computer and use it in GitHub Desktop.
def hourglassSum(arr):
results = []
max_value = 0
for j in range(4):
sums = []
for i in range(5):
sum_01 = sum(arr[j][i : 3 + i])
sum_02 = arr[j + 1][i + 1]
sum_03 = sum(arr[j + 2][i : 3 + i])
sums.append(sum_01 + sum_02 + sum_03)
max_of_sums = max(sums)
if max_of_sums >= max_value:
max_value = max_of_sums
results.append(sums)
print(results)
return max_value
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],
]
r = hourglassSum(input)
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment