aboutsummaryrefslogtreecommitdiff
path: root/challenge-086/richard-park/apl/ch-2.aplf
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 23)000,0, ⍝ Possible in subcells
     r{~9}1                       ⍝ Possible in rows
     c{~9}1                       ⍝ Possible in columns
     cells{                              ⍝ Initial contention map
         ⊃∩/(sc1 4 7),(r),c⊃⌽
     }¨9 9
     cells[0]@(0)cells           ⍝ Fill with known values
     Fill{cells
         {
             vcells
             v~c⌿⍨1=≢¨ccells[();(9)~⊃⌽]
             v~∊c⌿⍨1=≢¨ccells[(9)~⊃;⊃⌽]
         }¨9 9
     }
     solved¨Fillcells                   ⍝ Eliminate impossibilities
     PDE{(()⍴⍋⍋)()⍴⍋⍋}
     rows/,(9)PDE1solved
     columns/,(9)PDE1solved
     cells/,0 1 01{(9)PDE,}(2 23)000,0,solved
     rowscolumnscells:solved
     'Could not solve'
 }