30 Days of Code - Day 11: 2D Arrays

Coding challenges are a great resource for learning coding techniques and improve analytical thinking, this is a collection of challenges from different platforms.

Objective

Today, we’re building on our knowledge of Arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video!

Context:

Given a 6 x 6 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 00

input00.txt
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
output00.txt
19

Explanation

A contains the following hourglasses:

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

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

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

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

The hourglass with the maximum sum (19) is:

2 4 4
  2
1 2 4

Sample 07

input00.txt
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
output00.txt
19

Solution

main.go
package main

import "fmt"

func main() {
  A := [6][6]int{}

  for i := 0; i < 6; i++ {
    for j := 0; j < 6; j++ {
      fmt.Scan(&A[i][j])
    }
  }

  max := 0

  for i := 0; i < 4; i++ {
    for j := 0; j < 4; j++ {
      hgSum := sum(A[i][j:j+3]...) + A[i+1][j+1] + sum(A[i+2][j:j+3]...)

      if hgSum > max || i == 0 && j == 0 {
        max = hgSum
      }
    }
  }

  fmt.Println(max)
}

func sum(nums ...int) int {
  var result int

  for _, v := range nums {
    result += v
  }

  return result
}