let sq1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] let sq2 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]] let sq3 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] let sq4 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]] let sq5 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]] let sq6 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]] let sq7 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]] let sq8 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] let squares = [sq1, sq2, sq3, sq4, sq5, sq6, sq7, sq8] var cost = 0 var input = [[Int]]() for i in 0...2 { let values = readLine()!.characters.split{$0 == " "}.map(String.init) var level = [Int]() level.append(Int(values[0])!) level.append(Int(values[1])!) level.append(Int(values[2])!) input.append(level) } var minimumCost = 100 for s in 0...7 { let currentSquare = squares[s] var cost = 0 var movements = 0 for r in 0...2 { for c in 0...2 { let templateValue = currentSquare[r][c] let inputValue = input[r][c] if (templateValue != inputValue) { movements += 1 cost += abs(templateValue - inputValue) } } } //print("Movements : \(movements), cost: \(cost)") if (minimumCost > cost) { minimumCost = cost } } print(minimumCost)