Solving a Sudoku with 1 SQL-statement: the Model-clause

16

Solving a Suduku with one SQL-statement, is that possible? A lot of people won’t believe it, but yes, it can be done.....
I did already a blog on Solving a Sudoku with Collections, but for this blog I used another aproach: the model clause. The model clause is introduced in Oracle 10g and, according to the documentation "brings a new level of power and flexibility to SQL calculations". And that isn’t too much said! But how can you use it for solving Sudokus? Quit simple in fact Smiley, select it as a string from 81 characters from dual

select '&nbsp;&nbsp;&nbsp;56&nbsp;&nbsp;2 ' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;37' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 5&nbsp;&nbsp;&nbsp;&nbsp;173' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'327&nbsp;&nbsp;14&nbsp;&nbsp;' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;1&nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'6&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;2 381' || <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' s<br />from dual<br />

and add the model clause.

  model
    reference xxx on
      ( select i, j, r
        from dual
        model
          dimension by ( 1 i, 1 j )
          measures ( 1 x, 1 y, 1 r )
          rules
            ( x[for i from 1 to 81 increment 1, 1]= trunc( ( cv(i) - 1 ) / 9 ) * 9
            , y[for i from 1 to 81 increment 1, 1]= mod( cv(i) - 1, 9 ) + 1
            , r[for i from 1 to 81 increment 1, for j from 1 to 8 increment 1]= case when x[ cv(i), 1 ] + cv(j) &lt; cv(i)
                                                                                   then x[ cv(i), 1 ] + cv(j)
                                                                                   else x[ cv(i), 1 ] + cv(j) + 1
                                                                                 end
            , r[for i from 1 to 81 increment 1, for j from 9 to 16 increment 1]= case when y[ cv(i), 1 ] + ( cv(j) - 9 ) * 9 &lt; cv(i)
                                                                                    then y[ cv(i), 1 ] + ( cv(j) - 9 ) * 9
                                                                                    else y[ cv(i), 1 ] + ( cv(j) - 8 ) * 9
                                                                                  end
            , r[for i from 1 to 81 increment 1, 17]= case mod( x[ cv(i), 1 ] / 9, 3 )
                                                        when 0 then x[ cv(i), 1 ] + 9
                                                        when 1 then x[ cv(i), 1 ] - 9
                                                        when 2 then x[ cv(i), 1 ] - 18
                                                      end + mod( y[ cv(i), 1 ], 3 ) + trunc( (y[ cv(i), 1 ] - 1) / 3 ) * 3 + 1
            , r[for i from 1 to 81 increment 1, 18]= case mod( x[ cv(i), 1 ] / 9, 3 )
                                                        when 0 then x[ cv(i), 1 ] + 18
                                                        when 1 then x[ cv(i), 1 ] + 9
                                                        when 2 then x[ cv(i), 1 ] - 9
                                                      end + mod( y[ cv(i), 1 ], 3 ) + trunc( (y[ cv(i), 1 ] - 1) / 3 ) * 3 + 1
            , r[for i from 1 to 81 increment 1, 19]= case mod( x[ cv(i), 1 ] / 9, 3 )
                                                        when 0 then x[ cv(i), 1 ] + 9
                                                        when 1 then x[ cv(i), 1 ] - 9
                                                        when 2 then x[ cv(i), 1 ] - 18
                                                      end + mod( y[ cv(i), 1 ] + 1, 3 ) + trunc( (y[ cv(i), 1 ] - 1) / 3 ) * 3 + 1
            , r[for i from 1 to 81 increment 1, 20]= case mod( x[ cv(i), 1 ] / 9, 3 )
                                                        when 0 then x[ cv(i), 1 ] + 18
                                                        when 1 then x[ cv(i), 1 ] + 9
                                                        when 2 then x[ cv(i), 1 ] - 9
                                                      end + mod( y[ cv(i), 1 ] + 1, 3 ) + trunc( (y[ cv(i), 1 ] - 1) / 3 ) * 3 + 1
            )
      ) dimension by ( i, j ) measures ( r )

I use a reference model, which in turn is a SQL-query with a model clause. In a Sudoku can a certain number occur only once on every row, every column and every block. This reference model gives me for every cell in the Sudoku the numbers, that is the index in my Sudoku string, of the others cells on the same row, the same column and the same block. And that is all we need for the main model.

    main solve
      dimension by ( 1 x )
      measures ( cast( s as varchar2(81) ) s
               , 1 i
               , 1 j
               , 0 d
               , cast( '' as varchar2(20) ) v
               )
      rules iterate ( 100000 ) until ( length( replace( s[1], ' ' ) ) &gt;= 81 or d[1] + 81 &lt; iteration_number )
            ( i[1] = instr( s[1], '&nbsp;', j[1] )
            , v[1] = translate( '123456789'
                              , '#' ||
                                substr( s[1], xxx.r[ i[1], 1], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 2], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 3], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 4], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 5], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 6], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 7], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 8], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 9], 1 ) ||
                                substr( s[1], xxx.r[ i[1],10], 1 ) ||
                                substr( s[1], xxx.r[ i[1],11], 1 ) ||
                                substr( s[1], xxx.r[ i[1],12], 1 ) ||
                                substr( s[1], xxx.r[ i[1],13], 1 ) ||
                                substr( s[1], xxx.r[ i[1],14], 1 ) ||
                                substr( s[1], xxx.r[ i[1],15], 1 ) ||
                                substr( s[1], xxx.r[ i[1],16], 1 ) ||
                                substr( s[1], xxx.r[ i[1],17], 1 ) ||
                                substr( s[1], xxx.r[ i[1],18], 1 ) ||
                                substr( s[1], xxx.r[ i[1],19], 1 ) ||
                                substr( s[1], xxx.r[ i[1],20], 1 )
                              , '#' )
            , j[1] = case
                       when j[1] &gt;= 81 then 1
                       else j[1] + 1
                     end
            , s[1] = case
                       when length( v[1] ) = 1 then substr( s[1], 1, i[1] - 1 ) || v[1] || substr( s[1], i[1] + 1 )
                       else s[1]
                     end
            , d[1] = case
                       when length( v[1] ) = 1 then iteration_number
                       else d[1]
                     end
            )

And that means: find the first unsolved cell, starting from cell j

            ( i[1] = instr( s[1], '&nbsp;', j[1] )

for this cell, check the values from the other cells on the same row, column, block and find the possible values for this cell with the translate

            , v[1] = translate( '123456789'
                              , '#' ||
                                substr( s[1], xxx.r[ i[1], 1], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 2], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 3], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 4], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 5], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 6], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 7], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 8], 1 ) ||
                                substr( s[1], xxx.r[ i[1], 9], 1 ) ||
                                substr( s[1], xxx.r[ i[1],10], 1 ) ||
                                substr( s[1], xxx.r[ i[1],11], 1 ) ||
                                substr( s[1], xxx.r[ i[1],12], 1 ) ||
                                substr( s[1], xxx.r[ i[1],13], 1 ) ||
                                substr( s[1], xxx.r[ i[1],14], 1 ) ||
                                substr( s[1], xxx.r[ i[1],15], 1 ) ||
                                substr( s[1], xxx.r[ i[1],16], 1 ) ||
                                substr( s[1], xxx.r[ i[1],17], 1 ) ||
                                substr( s[1], xxx.r[ i[1],18], 1 ) ||
                                substr( s[1], xxx.r[ i[1],19], 1 ) ||
                                substr( s[1], xxx.r[ i[1],20], 1 )
                              , '#' )

and if only one value is possible we have solved this cell

            , s[1] = case
                       when length( v[1] ) = 1 then substr( s[1], 1, i[1] - 1 ) || v[1] || substr( s[1], i[1] + 1 )
                       else s[1]
                     end

and do this again and again until we have solved the complete Sudoku or don’t find anything to solve

      rules iterate ( 100000 ) until ( length( replace( s[1], ' ' ) ) &gt;= 81 or d[1] + 81 &lt; iteration_number )

Put this all together, and you have a query wich can solve only "simple" Sudokus. A slightly more complex version solves every possible Soduku, by trying every possible value in a cell, and try another possible value if the first gues proves to be wrong.

    rules iterate ( 999999 ) until ( length( replace( s[1], ' ' ) ) &gt;= 81 )
          ( i[ it[1] ] = case
                           when m[1] = 1 then instr( s[1], ' ' )
                           else i[ cv() ]
                         end
          , v[ it[1] ] = case
                           when m[1] = 1 then
                             translate( '123456789'
                                , '#' ||
                                  substr( s[1], xxx.r[ i[cv()], 1], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 2], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 3], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 4], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 5], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 6], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 7], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 8], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()], 9], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],10], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],11], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],12], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],13], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],14], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],15], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],16], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],17], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],18], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],19], 1 ) ||
                                  substr( s[1], xxx.r[ i[cv()],20], 1 )
                                , '#' )
                           else v[ cv() ]
                         end
          , m[1] = nvl2( v[ it[1] ], m[1], 0 )
          , it[1] = case
                      when m[1] = 1 then it[1]
                      else it[1] - 1
                    end
          , j[ it[1] ] = case
                           when m[1] = 1 then 1
                           else j[ cv() ] + 1
                         end
          , m[1] = case
                        when length( v[ it[1] ] ) &gt;= j[ it[1] ] then 1
                        else m[1]
                      end
          , s[1] = case
                      when m[1] = 1 then substr( s[1], 1, i[ it[1] ] - 1 ) || substr( v[ it[1] ], j[ it[1] ], 1 ) || substr( s[1], i[ it[1] ] + 1 )
                      else substr( s[1], 1, i[ it[1] ] - 1 ) || ' ' || substr( s[1], i[ it[1] ] + 1 )
                   end
          , it[1] = case
                      when m[1] = 1 then it[1] + 1
                      else it[1]
                    end
          )

This query can take some more time, up to 60 seconds.

These queries should work on every 10G database, but I have tested it only on 10.2.0.1.0, and I have heard that they won’t work on 10.2.0.3.0

Anton

code

Share.

About Author

16 Comments

  1. Sathya Narayanan on

    Great work Anton, I am trying hard to understand this but I have failed every time… Could you please explain the indexing that is  made in the reference model? Thank You

  2. Does anyone have this in one simple SQL, without it broken up into pieces? I can’t get it to execute.

    I assume I need the first part, select from dual, then the model clause (model reference xxx on …. dimension by (i,j) measures (r), then followed by the “main solve” clause?

    I keep getting syntax errors trying to run it, in 10.2.0.3.

    -Mark

  3. Mike Brandt on

    Well, that was very impressive. I am still working it out .

     

    By the way – if you like sql pluzzles you might like to try this, which can be solved by a single sql statement ( it will work even with Oracle 8i) , assuming you have a table called nums that holds the values 2..100.
    I like this problem , since it is almost impossible to solve by hand. By the way, the answer is the same even if you make the largest value 1000 (and then solving by hand becomes a nightmare).

    efine minval=2<br />define maxval=100<br />set numwidth 6<br />set pages 0<br />set feedback off<br />set verify off<br />column a3  format a3<br />column a15 format a15<br />column num format 9999<br />prompt<br />prompt Solve the problem set by:<br />prompt<br />prompt  There are 2 integers a and b between &amp;amp;&amp;amp;minval and &amp;amp;maxval, with a= a.num;<br /><br />create view sums as select a+b s,count(*) cc<br />       from pairs<br />       group by a+b;<br />      <br />create view products as select a*b p,count(*) cc<br />       from pairs<br />       group by a*b;<br /><br />prompt<br />prompt Paul: I don't know what the numbers are<br />prompt Sam : I knew that. I don't know what the numbers are either<br />prompt<br />prompt Paul's first statement is more helpful than it first appears<br />prompt The pair of numbers cannot be uniquely determined by their product<br />prompt This includes not just 2 primes such as 7 and 11, <br />prompt but also pairs like 19 and 38, or 4 and 53, 77 and 17<br />prompt (assuming we are only looking at 2 digit numbers)<br />prompt<br />prompt Sam's first statement is even more helpful.<br />prompt Sam knows that Paul can't possibly know the numbers<br />prompt So whatever his sum is, it cannot <br />prompt be formed by a pair that uniquely determines their product<br />prompt For example : the sum cannot be 18, or 57 or 94 using the above examples<br />prompt<br />prompt generate poss_sums as <br />prompt the only sums that cannot possibly be made by distinct product pairs<br />prompt There are surprisingly few<br />pause Ready?<br /><br />create view poss_sums as<br />  select s from sums where cc &amp;gt; 1<br />  minus<br />  select pairs.a+pairs.b s from pairs where pairs.a*pairs.b in<br />    (select p from products where cc=1);<br /><br />prompt<br />prompt From Sam's first statement the sum must be one of<br />prompt<br />select s from poss_sums<br />order by s;<br /><br />prompt<br />prompt Paul's last statement:<br />prompt Paul: I do now!<br />prompt<br />prompt So Paul knows that Sam knew that he did not know the numbers and so <br />prompt must have a sum matching the above criterion<br />prompt This enables him to deduce the pairs.<br />prompt So Paul must have a product that decomposes to one of the sums <br />prompt in the above list<br />prompt in exactly one way (2 or more would not give him a solution and 0 <br />prompt would violate Sam's previous statement)<br />prompt find all products that can have exactly one sum matching the list of sums above<br />prompt<br />pause Ready?<br /><br />create view poss_prods as<br />select products.p,max(poss_sums.s) s <br />from products,pairs,poss_sums<br />where products.cc &amp;gt; 1<br />  and pairs.a*pairs.b = products.p<br />  and pairs.a+pairs.b = poss_sums.s<br />group by products.p<br />having count(poss_sums.s) = 1;<br /><br />select 'product is ', p, 'sum is', s from poss_prods<br />order by p,s;<br /> <br />-- now find the sum that has only one possible product fitting that criterion <br /><br />prompt <br />prompt Sam's last statement:<br />prompt Sam : So do I!<br />prompt <br />prompt So we need to find a sum that only occurs once in the above list<br />prompt Otherwise Sam could still not deduce the pair.<br />prompt and as we know both the product and the sum we can find the pair<br />prompt<br />pause Ready?<br /><br />col plan_plus_exp for a120<br />set lines 160<br /><br />select 'In the range' a15,min(num) num,'to' a3,max(num) num from nums;<br /><br />set autotrace on explain<br /><br />select 'The numbers are' a15, a num,'and' a3, b num<br />from pairs,<br />   ( select max(p) p,s from poss_prods group by s having count(*) = 1) pp<br />where pairs.a*pairs.b = pp.p<br />  and pairs.a+pairs.b= pp.s<br />order by a,b;<br /><br />set autotrace off<br /><br />drop view poss_prods;<br />drop view poss_sums;<br /><br />drop view products;<br />drop view sums;<br />drop view pairs;<br /><br />prompt<br />prompt or to be perverse, and use only in-line views<br />pause Ready?<br />prompt<br /><br />-- using the with clause is much too slow in 10.1<br />-- with nums as (select rownum+&amp;amp;&amp;amp;minval-1 num from sys.tab$<br />--              where rownum = a.num<br />     )                                                          pairs,<br />     ( select max(p) p,s <br />       from ( select products.p,max(poss_sums.s) s <br />              from ( select a.num*b.num p,count(*) cc<br />                     from nums a, nums b<br />                     where b.num&amp;gt;= a.num<br />                     group by a.num*b.num<br />                     having count(*) &amp;gt;  1<br />                   )                                   products,<br />                   ( select a.num a, b.num b<br />                     from nums a, nums b <br />                     where b.num &amp;gt;= a.num<br />                   )                                   prs,<br />                   (<br />                     select s <br />                     from ( select a.num+b.num s<br />                            from nums a, nums b<br />                            where b.num &amp;gt;= a.num<br />                            group by a.num+b.num<br />                            having count(*) &amp;gt; 1 <br />                          )                            sums<br />                     minus<br />                     select prs2.a+prs2.b s <br />                     from ( select a.num a, b.num b<br />                            from nums a, nums b<br />                            where b.num &amp;gt;= a.num <br />                          )                            prs2 <br />                     where prs2.a*prs2.b in<br />                      ( select a.num*b.num p<br />                        from nums a, nums b<br />                        where b.num &amp;gt;= a.num<br />                        group by a.num*b.num<br />                        having count(*) = 1<br />                      )<br />                   )                                   poss_sums<br />              where prs.a*prs.b = products.p<br />                and prs.a+prs.b = poss_sums.s<br />              group by products.p<br />              having count(poss_sums.s) = 1<br />            ) <br />       group by s having count(*) = 1<br />     )                                                          pp<br />where pairs.a*pairs.b = pp.p<br />  and pairs.a+pairs.b= pp.s<br />order by a,b;<br /><br />set autotrace off<br />set timing off<br /><br />drop table nums; <br />

  4. I’m filing this one away for posterity.

    I hope it will work on Oracle 10XE because I would like to have this available to me on my laptop.