Share your repls and programming experiences

← Back to all posts
Go Solve Sudoku
splch (2)

suGOku

I just started using Go and would love for some Gophers to check out my Sudoku Solver :)

If you uncomment line 32, you can see the puzzle being solved!

printSudoku(*grid)

References

Comments
hotnewtop
splch (2)

{0, 4, 0, 9, 0, 0, 2, 5, 0},
{3, 2, 0, 0, 6, 4, 0, 1, 0},
{1, 0, 8, 0, 3, 7, 4, 0, 6},
{6, 7, 3, 0, 2, 9, 1, 0, 0},
{0, 0, 5, 0, 1, 0, 8, 0, 0},
{0, 0, 1, 4, 7, 0, 6, 3, 9},
{9, 0, 2, 7, 4, 0, 5, 0, 1},
{0, 3, 0, 1, 5, 0, 0, 6, 4},
{0, 1, 4, 0, 0, 8, 0, 0, 0},


{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},

Roar123 (442)

This is very cool! Even though the code looks like it came from the Python Sudoku Solver video, rewriting it in Go is still nice. Coming from a Python coder, how does it work?

splch (2)

@Roar123 Ya! I've coded in Python a bit so translating was a nice introduction to Go. The code is split into 2 major parts:
The first is the possible() function which determines if a number can be placed in the grid based on row, column, and 3x3 grid restrictions. The second part is using a recursive solve() function to try putting possible numbers in the grid! The video also introduced a cool idea called "backtracking" where you basically revert from a bad decision (as seen in line 35).
These two functions enable Go to solve sudoku!