A very simple learning application for Go.
When learning a new language, I often go for a Sudoku solver application. This is my attempt to put some of my recent Golang knowledge to work.
go build -o output/go-sudoku cmd/go-sudoku/main.go
./output/go-sudoku <file>
or
go run cmd/go-sudoku/main.go <file>
go test ./cmd/go-sudoku/...
There are a few benchmarks that are useful when making changes to the algorithm.
go test ./cmd/go-sudoku/solver/... -run=^$ -bench ^Benchmark
This section will describe the algorithm used to solve the puzzels. The basic idea is this.
- Find the possible options for each empty space
- Examine space-by-space for places with:
- Only one possible solution
- Only space in the row with a possible solution
- Only space in the column with a possible solution
- Only space in the cube with a possible solution
The application requires a file path parameter to a puzzle. The puzzle file should be nine lines long. Each line contains a space seperated list of entries. Each entry is either a number if given in the puzzle or a zero (0) to indicate the space is unsolved.
0 3 8 2 0 0 0 4 5
0 1 0 6 0 5 0 0 0
5 0 7 0 8 0 0 0 0
3 0 1 0 6 2 0 7 0
9 0 0 5 0 7 0 0 4
0 4 0 8 3 0 6 0 2
0 0 0 0 2 0 3 0 8
0 0 0 9 0 3 0 2 0
2 7 0 0 0 8 4 9 0