Skip to content

Instantly share code, notes, and snippets.

@Mardiniii
Last active April 15, 2018 16:50
Show Gist options
  • Save Mardiniii/80e79ed62c16674108db3489d0b63273 to your computer and use it in GitHub Desktop.
Save Mardiniii/80e79ed62c16674108db3489d0b63273 to your computer and use it in GitHub Desktop.
Code snippet for medium post
// Solve algorithm
func Solve(grid [N][N]int) (bool, [N][N]int) {
ch := make(chan [N][N]int)
pending, row, col := pendingPositions(grid)
if !pending {
fmt.Println("Solve calls:", count)
return false, grid
}
// Let's try to find a solution for the pending position
// in row and col. We will try with digits from 1 to 9
for i := 1; i <= 9; i++ {
if valid(grid, i, row, col) {
grid[row][col] = i
go recursiveSolution(grid, ch)
}
}
select {
case solution := <-ch:
fmt.Println("Solve calls:", count)
return true, solution
default:
return false, grid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment