Pencilling In the Possibilities
The most satisfying pencil mark in Sudoku is often the one you can erase. A digit appears elsewhere in the row, and a possibility quietly disappears. Repeat that for the column and the little 3-by-3 box, and the empty cell begins to tell you what it can hold.
That’s set difference: start with the digits 1 through 9 and remove every digit already present among the cell’s neighbours. Unlike ordinary subtraction, removing the same digit twice has no additional effect. A 4 in both the row and the box rules out 4 only once.
This BASIC example uses three groups of nine values: the row, column and box surrounding one empty cell. Zero represents an empty cell, so it doesn’t rule out a digit. The cell itself appears in all three groups, as it would when reading them from a grid.
DIM used(9)
DATA 0,1,3,4,5,6,8,9,0
DATA 0,4,5,6,8,9,0,0,0
DATA 0,1,3,4,5,6,8,9,0
FOR i = 1 TO 27
READ n
IF n <> 0 THEN used(n) = 1
NEXT i
FOR n = 1 TO 9
IF used(n) = 0 THEN PRINT n;
NEXT n
END
The output is 2 7. In a BASIC such as QB64, the array acts as a checklist: each observed digit gets a mark, and the unmarked digits remain candidates. The duplicate observations are harmless because marking an entry again leaves it marked.
Haskell can express the same difference directly, asking which digits are absent from the combined groups:
main :: IO ()
main = do
let row = [0,1,3,4,5,6,8,9,0]
column = [0,4,5,6,8,9,0,0,0]
box = [0,1,3,4,5,6,8,9,0]
seen = row ++ column ++ box
candidates = [n | n <- [1..9], n `notElem` seen]
print candidates
This prints [2,7]. The list version reads almost like the deduction itself, though it searches the observations for each digit; the checklist does a little more bookkeeping up front. For nine digits, either approach is comfortably small.
These are candidates, not a promise that both lead to a completed puzzle. Another deduction elsewhere may remove one later. For now, the useful result is knowing exactly which pencil marks survive this cell’s immediate neighbours.