Last active
April 15, 2018 16:50
-
-
Save Mardiniii/80e79ed62c16674108db3489d0b63273 to your computer and use it in GitHub Desktop.
Code snippet for medium post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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