RosettaCodeData/Task/Sudoku/Mathematica/sudoku-1.math

11 lines
369 B
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
solve[sudoku_] :=
2013-04-11 01:07:29 -07:00
NestWhile[
2015-11-18 06:14:39 +00:00
Join @@ Table[
Table[ReplacePart[s, #1 -> n], {n, #2}] & @@
First@SortBy[{#,
Complement[Range@9, s[[First@#]], s[[;; , Last@#]],
Catenate@
Extract[Partition[s, {3, 3}], Quotient[#, 3, -2]]]} & /@
Position[s, 0, {2}],
Length@Last@# &], {s, #}] &, {sudoku}, ! FreeQ[#, 0] &]