blob: fef60a75730112ccbcb229175b6b13f18c32727d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
Sudoku←{
⍝ Solve numeric 9×9 matrix sudoku puzzle
⍝ Only works if there is 1 solution
⍝ For more competent solvers, see https://dfns.dyalog.com/n_sudoku.htm
sc←{⍵~⍨⍳9}¨1 1↓⊂⍤⊢⌺(2 2⍴3)⊢0⍪0⍪0,0,⍵ ⍝ Possible in subcells
r←{⊂⍵~⍨⍳9}⍤1⊢⍵ ⍝ Possible in rows
c←{⊂⍵~⍨⍳9}⍤1⍉⍵ ⍝ Possible in columns
cells←{ ⍝ Initial contention map
⊃∩/(sc⌷⍨1 4 7⍸⍵),(r⌷⍨⊃⍵),c⌷⍨⊃⌽⍵
}¨⍳9 9
cells←⍵[⍸0≠⍵]@(⍸0≠⍵)⊢cells ⍝ Fill with known values
Fill←{cells←⍵
{
v←cells⊃⍨⊂⍵
v~←∊c⌿⍨1=≢¨c←cells[(⊃⍵);(⍳9)~⊃⌽⍵]
v~∊c⌿⍨1=≢¨c←cells[(⍳9)~⊃⍵;⊃⌽⍵]
}¨⍳9 9
}
solved←⊃¨Fill⍣≡cells ⍝ Eliminate impossibilities
PDE←{((≢⍺)⍴⍋⍋⍺⍳⍺⍪⍵)∊(≢⍵)⍴⍋⍋⍺⍳⍵⍪⍺}
rows←∧/,(⍳9)PDE⍤1⊢solved
columns←∧/,(⍳9)PDE⍤1⍉solved
cells←∧/,0 1 0↓1↓{(⍳9)PDE,⍵}⌺(2 2⍴3)⊢0⍪0⍪0,0,solved
rows∧columns∧cells:solved
'Could not solve'
}
|