Skip to content

Instantly share code, notes, and snippets.

@nticaric
Created February 5, 2018 23:20
Show Gist options
  • Save nticaric/b7fb2acb192073432139024bf8feb834 to your computer and use it in GitHub Desktop.
Save nticaric/b7fb2acb192073432139024bf8feb834 to your computer and use it in GitHub Desktop.
func isBoardValid(board *[9][9]int) bool {
//check duplicates by row
for row := 0; row < 9; row++ {
counter := [10]int{}
for col := 0; col < 9; col++ {
counter[board[row][col]]++
}
if hasDuplicates(counter) {
return false
}
}
//check duplicates by column
for row := 0; row < 9; row++ {
counter := [10]int{}
for col := 0; col < 9; col++ {
counter[board[col][row]]++
}
if hasDuplicates(counter) {
return false
}
}
//check 3x3 section
for i := 0; i < 9; i += 3 {
for j := 0; j < 9; j += 3 {
counter := [10]int{}
for row := i; row < i+3; row++ {
for col := j; col < j+3; col++ {
counter[board[row][col]]++
}
if hasDuplicates(counter) {
return false
}
}
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment