Repository navigation
Add a Sudoku example over an int[][] environment - #124
Open
HowardvanRooijen wants to merge 4 commits into
Open
HowardvanRooijen wants to merge 4 commits into
HowardvanRooijen wants to merge 4 commits into
Conversation
SudokuGrid holds the puzzle as one int[][] symbol, nine rows of nine, instead of the 81 scalar properties of SudokuTable. SudokuGridTheorem builds the same rules as SudokuTheorem - every cell between 1 and 9, and 27 Distinct constraints over the rows, columns and boxes - as expression trees whose cells are g.Cells[row][column] with the indices as literal constants, so nothing is captured from a closure. Given builds a given the same way. It shows a realistic-size use of nested collection symbols: 81 nested selects over one array, read back row by row. See #43. SudokuGridTheoremTests mirrors SudokuTheoremTests with the same published puzzle and expected solution, so the two can be compared: the known solution cell by cell, the grid's invariants with the givens preserved, and contradictory givens reporting unsatisfiable. Measured with a Stopwatch in Release, six runs each on that puzzle, building the theorem, solving and reading back: the int[][] theorem took 35-57 ms (150 ms on the first, cold run), the existing SudokuTheorem 34-44 ms. The nested array costs next to nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Invalid row or column indices can constrain off-grid elements, allowing solving to succeed while the returned grid silently omits a given, so coordinate validation is needed.
1 open issue
What changed in this PR
Adds an int[][] Sudoku example alongside the existing scalar-property example, demonstrating the nested collection support introduced in #123.
Changes:
- Introduces a pre-sized 9×9 grid with formatted text output.
- Builds cell-range, row, column, box, and given-cell constraints using expression trees with literal indices.
- Adds tests for the published solution, grid validity, preserved givens, and contradictory givens.
| File | Description |
|---|---|
solutions/Z3.Linq.Tests/SudokuGridTheoremTests.cs |
Tests solution correctness, Sudoku rules, and contradictory givens. |
solutions/Z3.Linq.Examples/Sudoku/SudokuGridTheorem.cs |
Builds Sudoku constraints over nested-array cells. |
solutions/Z3.Linq.Examples/Sudoku/SudokuGrid.cs |
Defines and formats the 9×9 nested-array environment. |
Review effort: Balanced
The grid is a Z3 array indexed by any integer, so Given(9, 0, 5) constrained an element the 9x9 grid never reads back, and the puzzle solved with the given silently missing. Given now throws ArgumentOutOfRangeException for a row or column outside 0 to 8. Found by review on #124. Checked by mutation: without the guard, all four out-of-grid cases of the new test fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Code Coverage Summary Report - Linux (No TFM)Summary
CoverageZ3.Linq - 93.9%
Z3.Linq.Examples - 70%
|
Test Results 1 files 1 suites 11s ⏱️ Results for commit 348bb93. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds a 9x9 Sudoku example whose environment is an
int[][]. This is the Sudoku from #43, rewritten in the style of the existingSudokuTable/SudokuTheoremexample.solutions/Z3.Linq.Examples/Sudoku/SudokuGrid.cs:int[][] Cells, set up as 9 rows of 9, with a gridToString().solutions/Z3.Linq.Examples/Sudoku/SudokuGridTheorem.cs:Createadds a 1-9 range constraint for each cell, and 27Distinctconstraints for the rows, columns and boxes.Given(row, column, value)builds the constraint for one given cell.ToString()and the logs stay readable.solutions/Z3.Linq.Tests/SudokuGridTheoremTests.cs: uses the same published puzzle and expected solution asSudokuTheoremTests, so the two examples can be compared.null.No demo, README or project file changes.
demos/sudoku.csalready shows Sudoku.Timing
Release build, same puzzle. Each run covers building the theorem, solving it and reading the answer back.
SudokuGridTheorem(int[][])SudokuTheorem(existing)The first
int[][]run is slower because it is the first solve in the process.Verification
dotnet build solutions/Z3.Linq.slnx: 0 warnings, 0 errors.dotnet test --solution solutions/Z3.Linq.slnx: 387 of 387 passed.🤖 Generated with Claude Code