Skip to content

Instantly share code, notes, and snippets.

@adrian17
Created March 5, 2015 09:24
Show Gist options
  • Save adrian17/b776863f3303c19782d5 to your computer and use it in GitHub Desktop.
Save adrian17/b776863f3303c19782d5 to your computer and use it in GitHub Desktop.
sudoku in J (translation of APL algorithm)
s44 =: 4 4 $ 0 0 0 0 0 0 2 1 3 0 0 4 0 0 0 0
box =: (] #"1 ] # ([: i. 2 # ]))
indices =: 3 : 0
yy =. {. y
a =. yy #"0 i. yy
b =. |: a
c =. a ,/"0 b
)
rcb =: 3 : 0
yy =. {. y
c =. indices y
rcb =. c ,/"(1 0) (box %: yy)
)
NB. <"1 (rcb 4 4)
cmap =: 1 e."1 (] ="(1 1)/ ])
NB. <"2 cmap rcb $ s44
avl =: 4 : '((1+i.{.$y) -. y * (<x) { cmap rcb $ y)'
NB.(tacit:) avl =: (1+[:i.[:{.[:$]) -. ] * ([:<[) { [: cmap [: rcb [: $ ]
NB. 2 1 avl s44
pvec =: 4 : 0
<"2 (x avl y) (<x) }/"(0 2) y
)
NB. 0 0 pvec s44
pvex =: [: ; [ ([: < pvec)"1 2 [: > ]
NB. 1 0 pvex 1 1 pvex 0 0 pvex s44
emt =: 3 : 0
a =. , ;/"2 indices $ y
b =. , 0 = s44
b # a
)
svec =: 3 : 'pvex&:>/ (emt y), <y'
svec s44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment