We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I wanted to do it the naive way without Googling what the valid permutations are.
packagemainimport("bufio""fmt""io""os""strconv""strings""math")funcsum(s[]int32)int32{sum:=int32(0)fori:=ranges{sum+=s[i]}returnsum}funcisMagic(s[][]int32)bool{sums:=[]int32{sum(s[0]),sum(s[1]),sum(s[2]),s[0][0]+s[1][0]+s[2][0],s[0][1]+s[1][1]+s[2][1],s[0][2]+s[1][2]+s[2][2],s[0][0]+s[1][1]+s[2][2],s[0][2]+s[1][1]+s[2][0],}fori:=rangesums{ifsums[i]!=15{returnfalse}}returntrue}funcpermutations(arr[]int32)[][]int32{varhelperfunc([]int32,int32)res:=[][]int32{}helper=func(arr[]int32,nint32){ifn==1{tmp:=make([]int32,len(arr))copy(tmp,arr)res=append(res,tmp)}else{fori:=0;i<int(n);i++{helper(arr,n-1)ifn%2==1{tmp:=arr[i]arr[i]=arr[n-1]arr[n-1]=tmp}else{tmp:=arr[0]arr[0]=arr[n-1]arr[n-1]=tmp}}}}helper(arr,int32(len(arr)))returnres}funcgenerateAllPossibleSquares()[][][]int32{squares:=permutations([]int32{1,2,3,4,5,6,7,8,9})validMagics:=make([][][]int32,8)validCounter:=0for_,s:=rangesquares{square:=[][]int32{s[0:3],s[3:6],s[6:9]}ifisMagic(square){validMagics[validCounter]=squarevalidCounter+=1}}returnvalidMagics}funccost(sq1[][]int32,sq2[][]int32)int32{totalCost:=int32(0)fori:=rangelen(sq1){forj:=rangelen(sq1[i]){totalCost+=int32(math.Abs(float64(sq1[i][j]-sq2[i][j])))}}returntotalCost}/* * Complete the 'formingMagicSquare' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY s as parameter. */funcformingMagicSquare(s[][]int32)int32{// First, we need a function that calculates all the various sums. // To find the closest magic square, we calculate the difference between each intersecting row// The smallest cost should be the difference between intersecting rowsminCost:=int32(math.MaxInt32)validMagics:=generateAllPossibleSquares()fori:=rangevalidMagics{newCost:=cost(s,validMagics[i])ifnewCost<minCost{minCost=newCost}}returnminCost}funcmain(){reader:=bufio.NewReaderSize(os.Stdin,16*1024*1024)stdout,err:=os.Create(os.Getenv("OUTPUT_PATH"))checkError(err)deferstdout.Close()writer:=bufio.NewWriterSize(stdout,16*1024*1024)vars[][]int32fori:=0;i<3;i++{sRowTemp:=strings.Split(strings.TrimRight(readLine(reader)," \t\r\n")," ")varsRow[]int32for_,sRowItem:=rangesRowTemp{sItemTemp,err:=strconv.ParseInt(sRowItem,10,64)checkError(err)sItem:=int32(sItemTemp)sRow=append(sRow,sItem)}iflen(sRow)!=3{panic("Bad input")}s=append(s,sRow)}result:=formingMagicSquare(s)fmt.Fprintf(writer,"%d\n",result)writer.Flush()}funcreadLine(reader*bufio.Reader)string{str,_,err:=reader.ReadLine()iferr==io.EOF{return""}returnstrings.TrimRight(string(str),"\r\n")}funccheckError(errerror){iferr!=nil{panic(err)}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
I wanted to do it the naive way without Googling what the valid permutations are.