add new solutions
This commit is contained in:
parent
62ed89abcc
commit
ed8cd12d25
2 changed files with 89 additions and 0 deletions
63
solutions/q36/solution.go
Normal file
63
solutions/q36/solution.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package q36
|
||||
|
||||
func validateCol(board [][]byte, col int) bool {
|
||||
var present [10]bool
|
||||
for i := range 9 {
|
||||
c := board[i][col]
|
||||
if c != '.' {
|
||||
if present[c-'0'] {
|
||||
return false
|
||||
}
|
||||
present[c-'0'] = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validateRow(board [][]byte, row int) bool {
|
||||
var present [10]bool
|
||||
for i := range 9 {
|
||||
c := board[row][i]
|
||||
if c != '.' {
|
||||
if present[c-'0'] {
|
||||
return false
|
||||
}
|
||||
present[c-'0'] = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validateBox(board [][]byte, box int) bool {
|
||||
xOffset := (box % 3) * 3
|
||||
yOffset := (box / 3) * 3
|
||||
|
||||
var present [10]bool
|
||||
for i := range 9 {
|
||||
x := xOffset + i%3
|
||||
y := yOffset + i/3
|
||||
c := board[y][x]
|
||||
if c != '.' {
|
||||
if present[c-'0'] {
|
||||
return false
|
||||
}
|
||||
present[c-'0'] = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func validate(board [][]byte) bool {
|
||||
for i := range 9 {
|
||||
if !validateCol(board, i) || !validateRow(board, i) || !validateBox(board, i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isValidSudoku(board [][]byte) bool {
|
||||
return validate(board)
|
||||
}
|
||||
|
||||
var _ = isValidSudoku
|
||||
Loading…
Add table
Add a link
Reference in a new issue