Before solving anything, you need to know whether a grid even obeys the rules. Today you write the validity check - no digit repeated in any unit - closing the board chapter with something you can run on a real puzzle.
Report whether a grid has any duplicate digit within a row, column, or box.
Validity is the one hard rule of Sudoku expressed directly: within each of the
27 units, no digit may appear more than once. Blanks are skipped - a 0 is not yet
a commitment, so it can never clash. A grid passes only if every unit is
clash-free, which is exactly what looping over the units and watching for a repeated
digit checks. Note that “valid” is weaker than “solvable”: an empty grid is
perfectly valid, and so is a partly-filled one that happens to have no solution, as
long as nothing repeats yet.
This is the first genuinely useful thing the library does with the units and the board together, and it closes the opening chapter. You can now load a puzzle and confirm it is well-formed before spending any effort trying to solve it - and every step the solver takes will keep this invariant true.
// for each unit, no non-zero digit may appear twicefunc IsValid(g [81]int) bool {for _, u := range Units() {seen := map[int]bool{}for _, c := range u {v := g[c]if v == 0 { continue } // blanks never conflictif seen[v] { return false } // repeat inside this unitseen[v] = true}}return true}