This is a beginner warm up hacker rank exercise. If anybody is up for it, I am interested in hearing feedback on the code. I am new to go and I’m trying to improve my instincts around writing idiomatic code:
func hourglasSumAt(arr [][]int32, row int, col int) int32 {
r0, r2 := row - 1, row + 1
c0, c2 := col - 1, col + 1
return (arr[r0][c0] + arr[r0][col] + arr[r0][c2] +
arr[row][col] +
arr[r2][c0] + arr[r2][col] + arr[r2][c2])
}
func hourglassSum(arr [][]int32) int32 {
max := int32(math.MinInt32)
var row1len int
if len(arr) > 0 {
row1len = len(arr[0])
}
for r := 1; r < len(arr) - 1; r++ {
if len(arr[r]) != row1len {
log.Panic("All rows do not have", row1len, "cols. Found row with", len(arr[r]))
}
for c := 1; c < row1len - 1; c++ {
if s := hourglasSumAt(arr, r, c); s > max {
max = s
}
}
}
return max
}