IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Forces of DB2 nature: SUDOKU Solver (in seconds)


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Sep 09, 2009 1:15 pm
Reply with quote

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
               )   )
        )

select g.* from test g   
where lvl = 81
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Sep 09, 2009 1:43 pm
Reply with quote

ps.: There is no need to mention my name if you want to use this SQL. Just don't claim it to be yours.
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Thu Sep 10, 2009 6:00 am
Reply with quote

GuyC wrote:
ps.: There is no need to mention my name if you want to use this SQL. Just don't claim it to be yours.


I did not test it yet, but if it working, I like it.

I like to see different solution of this difficult problem. icon_exclaim.gif

Thanks, Lenny
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Sun Sep 13, 2009 6:57 pm
Reply with quote

Sudoku Solver is working, but this is not enough for me.

Now I have to make the Sudoku Builder.

If somebody has some ideas let me know.

Thanks, Lenny
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Thu Sep 24, 2009 7:45 am
Reply with quote

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.

Sudoku Line

Quote:
XXX7X29X4
4X1X687XX
XXXXXXXX8
6X7XX142X
XXXXXXXXX
X236XX1X5
1XXXXXXXX
XX827X5X1
7X24X6XXX


Result Lines

Quote:
Line 1: | 8 | 6 | 5 | 7 | 3 | 2 | 9 | 1 | 4 |
Line 2: | 4 | 3 | 1 | 9 | 6 | 8 | 7 | 5 | 2 |
Line 3: | 2 | 7 | 9 | 1 | 4 | 5 | 6 | 3 | 8 |
Line 4: | 6 | 8 | 7 | 5 | 9 | 1 | 4 | 2 | 3 |
Line 5: | 5 | 1 | 4 | 3 | 2 | 7 | 8 | 9 | 6 |
Line 6: | 9 | 2 | 3 | 6 | 8 | 4 | 1 | 7 | 5 |
Line 7: | 1 | 9 | 6 | 8 | 5 | 3 | 2 | 4 | 7 |
Line 8: | 3 | 4 | 8 | 2 | 7 | 9 | 5 | 6 | 1 |
Line 9: | 7 | 5 | 2 | 4 | 1 | 6 | 3 | 8 | 9 |


Code in attachment (in another topic):

Leonid Khiger
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Sep 24, 2009 7:47 pm
Reply with quote

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.

Just a caution. . .
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Fri Sep 25, 2009 5:44 am
Reply with quote

dick scherrer wrote:
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.

Just a caution. . .

This is not a SPU time, but elapse time. icon_redface.gif

Do not use this product if it too expansive for you.

Use Sudoku killer which works 60 times faster. icon_wink.gif

Yours Lenny
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Fri Sep 25, 2009 6:45 am
Reply with quote

You can find the last version here:

www.ibmmainframes.com/viewtopic.php?t=44004

This one do not eat CPU time.

Lenny
Back to top
View user's profile Send private message
ashutosh.pr

New User


Joined: 13 Apr 2007
Posts: 36
Location: Pune

PostPosted: Fri Sep 25, 2009 9:39 am
Reply with quote

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
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Fri Sep 25, 2009 4:09 pm
Reply with quote

ashutosh.pr wrote:
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. icon_lol.gif

Now I have some difficulties with SUDOKU Builder and this is also fun. icon_idea.gif

Lenny
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2 Goto page 1, 2  Next

 


Similar Topics
Topic Forum Replies
No new posts Is Addressing Issue depends on nature... COBOL Programming 7
No new posts Sort: Does INREC forces a "pass ... JCL & VSAM 10
No new posts DB2 Sql to perform commit operation e... DB2 7
No new posts Summing Minutes:Seconds on a file? DFSORT/ICETOOL 2
No new posts Timestamp difference in seconds - DB2... DB2 2
Search our Forums:

Back to Top