package main import "fmt" func abs(x int) int { if x < 0 { x *= -1 } return x } func main() { inSquare := make([]int, 9) var s = [8][9]int{{8,1,6,3,5,7,4,9,2}, {4,9,2,3,5,7,8,1,6}, {6,1,8,7,5,3,2,9,4}, {2,9,4,7,5,3,6,1,8}, {4,3,8,9,5,1,2,7,6}, {8,3,4,1,5,9,6,7,2}, {2,7,6,9,5,1,4,3,8}, {6,7,2,1,5,9,8,3,4}} for i := range inSquare { fmt.Scan(&inSquare[i]) } shortestDist := 1000 var currentDist int for i := 0; i < 8; i++ { currentDist = 0 for j := 0; j < 9; j++ { currentDist += abs(s[i][j] - inSquare[j]) } if currentDist < shortestDist { shortestDist = currentDist } } fmt.Println(shortestDist) }