Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MinSomai/0c21dea613350721d425168ff4ca5970 to your computer and use it in GitHub Desktop.
Save MinSomai/0c21dea613350721d425168ff4ca5970 to your computer and use it in GitHub Desktop.
Day 11 : Golang | 2D Array - Hackerrank.go
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func hourglassWithMaxSum(arr [][]int32){
var highestHourglassSum int32 = 0;
for i := 0; i < 4; i++ {
for j:=0; j < 4; j++ {
firstCol := arr[i][j] + arr[i][j+1] + arr[i][j+2]
secondCol := arr[i+1][j+1]
thirdCol := arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]
currentHourGlass := firstCol + secondCol + thirdCol;
if i==0 && j==0 && currentHourGlass < 0{
highestHourglassSum = currentHourGlass
}
if currentHourGlass > highestHourglassSum{
highestHourglassSum = currentHourGlass
}
}
}
fmt.Println(highestHourglassSum)
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024 * 1024)
var arr [][]int32
for i := 0; i < 6; i++ {
arrRowTemp := strings.Split(readLine(reader), " ")
var arrRow []int32
for _, arrRowItem := range arrRowTemp {
arrItemTemp, err := strconv.ParseInt(arrRowItem, 10, 64)
checkError(err)
arrItem := int32(arrItemTemp)
arrRow = append(arrRow, arrItem)
}
if len(arrRow) != int(6) {
panic("Bad input")
}
arr = append(arr, arrRow)
}
hourglassWithMaxSum(arr)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment