I gave it a try :
Simple brute force , 40 lines of code
one table with 9 rows : all numbers
one recursion : each recursion fills in one more position
as recursion reaches 81 all positions are filled in.
Code:
with all_nbrs (k, ck) as
( select 1, '1' from sysibm.sysdummy1
union all
select k + 1, substr(char(k + 1),1,1) from all_nbrs where k + 1 <= 9 )
, test(lvl,sn) as
(select 0 , '090100000'
!! '037020000'
!! '060908004'
!! '009070000'
!! '206000107'
!! '000050300'
!! '700603050'
!! '000080630'
!! '000002040 ' from sysibm.sysdummy1
union all
select lvl + 1
, case when lvl= 0 then '' else substr(sn,1,lvl) end
!! a.ck
!! substr(sn,lvl+2)
from test t join all_nbrs a on 1=1
where lvl < 81
and ( substr(sn,lvl+1,1) = ck
or (substr(sn, lvl+1 ,1) = '0'
and locate( ck,substr(sn , int(lvl / 9)*9+1 ,9) ) = 0 -- ROW
and ck not in ( substr(sn , mod(lvl,9)+1 ,1)
, substr(sn , mod(lvl,9)+10 ,1)
, substr(sn , mod(lvl,9)+19 ,1)
, substr(sn , mod(lvl,9)+28 ,1)
, substr(sn , mod(lvl,9)+37 ,1)
, substr(sn , mod(lvl,9)+46 ,1)
, substr(sn , mod(lvl,9)+55 ,1)
, substr(sn , mod(lvl,9)+64 ,1)
, substr(sn , mod(lvl,9)+73 ,1) ) -- COL
and locate( ck , substr(sn , int(lvl/27)*27 + int(mod(lvl,9) / 3 ) * 3 + 1 ,3) ) = 0
and locate( ck , substr(sn , int(lvl/27)*27 + int(mod(lvl,9) / 3 ) * 3 + 10 ,3) ) = 0
and locate( ck , substr(sn , int(lvl/27)*27 + int(mod(lvl,9) / 3 ) * 3 + 19 ,3) ) = 0 -- BLOCK
) )
)
I want present to you Sudoku Solver, created by me today, which find solution for any level of Sudoku in very short time from 2 to 80 seconds, depends on difficulty level.
This solution prevents from infinite loop. So if solution exists it'll be found.
In this example I used the very hard sudoku and got solution in 2 minutes.
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
Hello,
Suggest that 2 minutes of cpu time is not appropriate for running a game (not counting the time used testing). System resources are not free. . . Unless maybe you/your family own the company. . .
Several of the places i've supported would find this waste grounds for termination.
Suggest that 2 minutes of cpu time is not appropriate for running a game (not counting the time used testing). System resources are not free. . . Unless maybe you/your family own the company. . .
Several of the places i've supported would find this waste grounds for termination.
Just a caution. . .
This is not a SPU time, but elapse time.
Do not use this product if it too expansive for you.
Sorry for going offtrack here, but I believe Sudoku's purpose is to test one's calibre to play with numbers,to engage the time when someone is sitting idle and most importantly to have fun.
Sorry for going offtrack here, but I believe Sudoku's purpose is to test one's calibre to play with numbers,to engage the time when someone is sitting idle and most importantly to have fun.
I feel some things are better when not automated.
Nothing against your coding skills, Lenny.
Thanks,
Ashutosh
I am agree with you. But I had a big fun as programmer, when I created different algorithms and forced programs to work.
Now I have some difficulties with SUDOKU Builder and this is also fun.