forked from Shopify/infra-intern-assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.go
195 lines (172 loc) · 4.13 KB
/
sudoku.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import "fmt"
const dim = 9
const emptyCell = 0
// solves the Sudoku puzzle using backtracking
func solve(grid [][]int) bool {
row, col, found := findEmptyCell(grid)
// check if successful
if !found {
return true
}
// Filling empty cells
for num := 1; num <= dim; num++ {
if checkIfSafe(grid, row, col, num) {
grid[row][col] = num
// recursive step to solve puzzle
if solve(grid) {
return true
}
// If solution is invalid, backtrack
grid[row][col] = emptyCell
}
}
return false
}
// finds the position of empty cells
func findEmptyCell(grid [][]int) (int, int, bool) {
for row := 0; row < dim; row++ {
for col := 0; col < dim; col++ {
if grid[row][col] == emptyCell {
return row, col, true
}
}
}
return -1, -1, false
}
// checks if specified value is present in the given row
func checkRow(grid [][]int, row int, val int) bool {
for columnIndex := 0; columnIndex < dim; columnIndex++ {
if grid[row][columnIndex] == val {
return true
}
}
return false
}
// checks if specified value is present in the given column
func checkCol(grid [][]int, col int, val int) bool {
for rowIndex := 0; rowIndex < dim; rowIndex++ {
if grid[rowIndex][col] == val {
return true
}
}
return false
}
// checks if specified value is present in the given section
func checkSection(grid [][]int, row int, col int, val int) bool {
for rowIndex := 0; rowIndex < 3; rowIndex++ {
for colIndex := 0; colIndex < 3; colIndex++ {
if grid[rowIndex+row][colIndex+col] == val {
return true
}
}
}
return false
}
// checks whether the given value is able to be placed in the given position
func checkIfSafe(grid [][]int, row int, col int, val int) bool {
return grid[row][col] == emptyCell && !checkRow(grid, row, val) && !checkCol(grid, col, val) && !checkSection(grid, row-row%3, col-col%3, val)
}
// prints the grid
func printGrid(grid [][]int) {
for rowIndex := 0; rowIndex < dim; rowIndex++ {
for colIndex := 0; colIndex < dim; colIndex++ {
fmt.Printf("%d ", grid[rowIndex][colIndex])
}
fmt.Println()
}
}
// solves the puzzle
func SolveSudoku(input [][]int) [][]int {
// checks if input is 9x9
if len(input) != 9 {
return nil
}
for _, row := range input {
if len(row) != 9 {
return nil
}
}
if !isValidInput(input){
return nil
}
if !solve(input) {
return nil
}
return input
}
func isValidInput(grid [][]int) bool {
for i := 0; i < 9; i++ {
if !checkValidRow(grid, i) || !checkValidCol(grid, i) || !checkValidSection(grid, i) {
return false
}
}
return true
}
// checks if the specified row is valid
func checkValidRow(grid [][]int, row int) bool {
visited := make(map[int]bool)
for _, value := range grid[row] {
if value != 0 {
if visited[value] {
return false
}
visited[value] = true
}
}
return true
}
// checks if the specified column is valid
func checkValidCol(grid [][]int, col int) bool {
visited := make(map[int]bool)
for row := 0; row < 9; row++ {
value := grid[row][col]
if value != 0 {
if visited[value] {
return false
}
visited[value] = true
}
}
return true
}
// checks if the specified 3x3 box is valid
func checkValidSection(grid [][]int, box int) bool {
visited := make(map[int]bool)
rowOffset := (box / 3) * 3
colOffset := (box % 3) * 3
// section row
for i := 0; i < 3; i++ {
// section column
for j := 0; j < 3; j++ {
value := grid[rowOffset+i][colOffset+j]
if value != 0 {
if visited[value] {
return false
}
visited[value] = true
}
}
}
return true
}
// main function, used for debugging
func main() {
grid := [][]int {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9},
}
solved := SolveSudoku(grid)
if solved != nil {
printGrid(solved)
return
}
fmt.Println("Cannot be solved")
}