I have been trying to adapt Roger Hui's sudoku solver
https://code.jsoftware.com/wiki/Essays/Sudoku to a 4x4 grid from his 3x3
grid. I actually do not want such an automatic solution, and prefer a
solution which helps a human with some of the tedium, but in the meantime,
I think I would like an automatic solution as well.
My problem seems to be that my adaptation does not screen adequately for
multiple identical "candidates". By that I mean in the following example,
for example, the "candidate" 3 appears three times, (by coincidence), as a
"solution" produced by the repeated application of the verb 'f', and in
particular, here, in the second update of the grid. To see what I mean,
execute the following commands on the attached script.
load'~user/sudoku16forum.txt'
9!:7'+++++++++|-'
f=: + (ac >. ar)@free NB. one step of assign
$,.see t=: f^:a: x NB. forced moves leading from grid x
3{.see diff t
In particular, look at the following result which shows 3 boxes (which I
hope are not wrapped by your email client, but if it is wrapped, you might
just look at 1 2&{ instead of 3&{. ).
In the third box, focus on the bottom righthand 4x4 region which contains
the characters E330 and '.'s . In that region itself there are two 3's,
which should not happen. But in addition each of those 3's collide with
external 3's in their own row or column, but outside the 4x4 region we are
focusing on. [I have attempted to produce the 3's below in *bold* type.]
I can't tell if this problem is a result of something I have done wrong in
my adaptation or if there is more coding required for 4x4 relative to 3x3,
or what.
3{.see diff t
+---------------------+---------------------+---------------------+
|+----+----+----+----+|+----+----+----+----+|+----+----+----+----+|
||5E..|2.1.|.F6.|039.|||....|....|....|...C|||....|....|....|....||
||....|5.D.|0E..|....|||....|....|....|.2..|||....|....|....|...1||
||.28E|6F3.|4...|B...|||....|...0|....|.D..|||....|....|....|....||
||.F.0|....|D...|.48.|||....|....|....|E...|||....|..A.|..2.|....||
|+----+----+----+----+|+----+----+----+----+|+----+----+----+----+|
||.5C.|.1.3|2...|A9BD|||....|....|..0E|....|||....|....|....|....||
||..A.|4...|1...|50..|||....|....|....|...2|||....|...6|....|..E.||
||1...|...E|..F2|...F|||....|....|9...|4C..|||....|....|....|..*3*.||
||.9.6|A.2.|..D.|7816|||..E.|....|....|....|||0...|....|...4|....||
|+----+----+----+----+|+----+----+----+----+|+----+----+----+----+|
||D1.8|...C|.49.|3.2.|||....|....|....|...0|||....|.E..|....|.6..||
||..09|..C.|....|.5..|||.6..|.A..|...D|....|||....|....|E...|....||
||73..|.2..|.A.6|..DE|||....|....|....|....|||....|..8.|....|....||
||AC..|.6B.|5..0|9..A|||..E.|....|....|....|||....|....|....|....||
|+----+----+----+----+|+----+----+----+----+|+----+----+----+----+|
||3...|.9..|..7.|D...|||.0..|...2|....|....|||....|....|....|.E..||
||.4..|.0.1|6DE9|2...|||....|....|....|....|||8.5.|*3*...|....|...*3*||
||....|E7.8|.0..|...7|||....|....|....|....|||..2.|....|....|..*3*.||
||EB.F|8..A|.2..|C1..|||..7.|....|3...|...9|||....|....|....|..0.||
|+----+----+----+----+|+----+----+----+----+|+----+----+----+----+|
+---------------------+---------------------+---------------------+
--
(B=) <-----my sig
Brian Schott
raw=: 0 : 0
5E 2 1 F6 039 5 D 0E 28E6F3 4 B F 0 D 48 5C 1 32
A9BD A 4 1 50 1 E F2 F 9 6A 2 D 7816D1 8 C 49 3 2 09 C
5 73 2 A 6 DEAC 6B 5 09 A3 9 7 D 4 0 16DE92 E7 8 0
7EB F8 A 2 C1
)
$craw=:_16]\}:raw
]all=:a.{~32,(48+i.10),65+i.6
x=:,all i. craw
NB. https://code.jsoftware.com/wiki/Essays/Sudoku adaptation
j =. (]/. i.@#) ,{;~4#i.4
r =. 16#i.16 16
c =. 256$|:i.16 16
b =. (,j{16#i.16) { j
I =: ~."1 r,.c,.b
R =: j,(,|:)i.16 16
regions=: R {"_ 1 ]
free =: 0&= > (1+i.16) e."1 I&{
ok =: (48 16$1) -:"2 (0&= +. ~:"1)@regions
ac =: +/ .*&(1+i.16) * 1 = +/"1
ar =: 3 : 0
m=. 1=+/"2 R{y
j=. I. +./"1 m
k=. 1 i."1~ j{m
i=. ,(k{"_1 |:"2 (j{R){y) #"1 j{R
(1+k) i}256$_1
)
assign =: (+ (ac >. ar)@free)^:_"1
guessa =: 3 : 0
if. -. 0 e. y do. ,:y return. end.
b=. free y
i=. (i.<./) (+/"1 b){16,}.i.16
y +"1 (1+I.i{b)*/i=i.81
)
guess =: ; @: (<@guessa"1)
sudoku =: guess @: (ok # ]) @: assign ^:_ @ ,
see1 =: (;~16$1 0 0 0)&(<;.1) @ ({&'.0123456789ABCDEF') @ (16 16&$) @ ,
see =: <@see1"1`see1@.(1:=#@$)
diff =: * 0&=@}:@(0&,)
Note 'demo suggestions'
z=:9!:6''
9!:7'+++++++++|-'
f=: + (ac >. ar)@free NB. one step of assign
,.see t=: f^:a: x NB. forced moves leading from grid x
3{.see diff t NB. differences from one grid to the next
see assign x NB. same as the last grid above
see g=: guess (ok#]) assign x NB. guesses after exhausting forced moves
see t0=: f^:a: 0{g NB. forced moves leading from guess 0
see diff t0 NB. differences from one grid to the next
see t1=: f^:a: 1{g NB. forced moves leading from guess 1
see diff t1 NB. differences from one grid to the next;
NB. note the obviously invalid assignments
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm