Open DylanSp opened 9 months ago
Summary of solver algorithm from Norvig post:
I'll probably want to make a Set[T]
struct for easily representing sets of possible values that's more ergonomic than map[T]struct{}
.
map[T]struct{}
.Add(element T)
Has(element T) bool
Delete(element T)
Clear()
- remove all elementsEntries() []T
(not sure on name) - return all elements in the setmap
is nil
, so initialize it to an empty map if it's nil
).When designing the needed data structures for representing the grid, possible solutions, and so on - look at ideas from Philosophy of Software Design, such as "Design it twice".
Data structures from Bendersky's codebase:
Set[T]
struct added in https://github.com/DylanSp/sudoku-toolkit/commit/4a23e237fe6c48846aa79a90e5f10890f1b87230.
Parser for 4x4 puzzle added in https://github.com/DylanSp/sudoku-toolkit/commit/36b3d89db3d31620600231c450fde165ca762d26, as well as some examples, for easier testing.
As of https://github.com/DylanSp/sudoku-toolkit/commit/94665c4fb7b1083e2eee4444868d5271fc69eb7f, I've implemented a solver that works with just the basic strategies (eliminating all possibilities that are already on the board, filling in cells with only a single possible value). This works correctly for the 4x4 example puzzles and the first 9x9 puzzle in easy50.txt, so its logic is correct. It's not sufficient to solve the second 9x9 puzzle in easy50.txt, so that can be used to test the backtracking solver.
Started working on backtracking search in https://github.com/DylanSp/sudoku-toolkit/commit/22240a768f536b35c96d7cbd6e02352059ab878d - needs to be able to detect when a chosen search path has lead to a contradiction (some empty cell has no remaining valid possibilities), then return false
in that case.
Probably create a function to check if a puzzle has a contradiction (figure out a better name), call it at this point in the search loop - https://github.com/DylanSp/sudoku-toolkit/blob/main/sudoku/solver.go#L76.
As of https://github.com/DylanSp/sudoku-toolkit/commit/2ffce05d6533f7b6233ce425ffe383142bbbf3f4 on the immutable-puzzle
branch, backtracking search works on all puzzles in the easy50 list, though there's a point where I apparently have incorrect assumptions about my code's behavior - I want to understand that before merging into main
.
Use basic techniques, just test on 4x4 and 9x9 boards