FUN only, solving a Puzzle.

  • /*

    For FUN.

    I like puzzles, I like T-SQL coding, I like to show off :-), so why not combine these.

    This project was for FUN only, but I did learn new things.

    Goal:

    Build a Sudoku solver with the minimum amount of script. (Minimum number of lines or characters).

    It does have to solve any normal Sudoku, speed is not a requirement (but would be nice).

    Elegance of the coding is not a requirement (but would be nice).

    Preferable with the minimum of RBAR (Row By Agonizing Row).

    Solution:

    When given a puzzle, I like to solve the puzzle before a solution is presented.

    Not to rob you of attempting this puzzle, I will post my solution later, with a description.

    I have used previous versions of the 'solution' and other puzzles , to show that set based solutions can be a powerful method to solve 'problems'.

    Ben

    */

  • ben.brugman wrote:

    /*

    For FUN.

    I like puzzles, I like T-SQL coding, I like to show off :-), so why not combine these.

    This project was for FUN only, but I did learn new things.

    Goal:

    Build a Sudoku solver with the minimum amount of script. (Minimum number of lines or characters).

    It does have to solve any normal Sudoku, speed is not a requirement (but would be nice).

    Elegance of the coding is not a requirement (but would be nice).

    Preferable with the minimum of RBAR (Row By Agonizing Row).

    Solution:

    When given a puzzle, I like to solve the puzzle before a solution is presented.

    Not to rob you of attempting this puzzle, I will post my solution later, with a description.

    I have used previous versions of the 'solution' and other puzzles , to show that set based solutions can be a powerful method to solve 'problems'.

    Ben

    */

     

    Ben, I'm sure this has already been done here, and the final solution is as skimpy as Taylor Swift's bikini.

     

    Edited to add link: https://jasonbrimhall.info/2011/08/23/tsql-sudoku-ii/

     

    • This reply was modified 4 years, 2 months ago by  ChrisM@Work.
    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work wrote:

    Ben, I'm sure this has already been done here, and the final solution is as skimpy as Taylor Swift's bikini.

    Edited to add link: https://jasonbrimhall.info/2011/08/23/tsql-sudoku-ii/

    Thank you Chris,

    Especially for the link, had not seen that one before. But as puzzle's go, there is bound to be people who have solved the puzzle before. As for Sudoku's, the solve is in the next issue of the paper or magazine, or sometimes in the same one. So there is no problem in that the puzzle has been solved before.

    The link to the solution, looks a bit like my solution. It contains 1591 characters, after removing (most) of the white space and comments it contains something like just below a 1000 characters. (I got to 998 for this script but did not spend to much time on it).

    I also took the solution and did run it. And tried the same Sudoku (starting with '53') and tried that on  my script. I was surprised that the published script ran in just over 1.5 secs on my machine and my script ran in 0.16 on my machine. My solution was totally geared to the minimum script and not to performance. So in performance (for this puzzle) I did beat the published solution. (Do not know why).

    The length of the published script (just below 1000 chars), but can be trimmed down a bit, my solution does contain less lines if formatted logically. And does contain less characters. It is less than the skimpy, probably only a swimming trunk. (Correct word ? English is not my native language). (It is not more, but more skimpy, is that a contradiction).

    Still working on trimming my script down, still figuring out to remove some length, have some idea's but some of them didn't work out. The length at the moment is significant below a 1000 chars.

    Still the same goes: So I do not want to spoil the puzzle and will be posting the script and a description later.

    Thanks for the link, I enjoyed that,

    Ben

     

     

     

     

    • This reply was modified 4 years, 2 months ago by  ben.brugman. Reason: typo
  • This puzzle was solved many years ago by Richard Romley. He was an SQL programmer at a Wall Street brokerage firm who likes to write solutions to SQL puzzles. Back when I had my column in DBMS magazine, I would end each column with an SQL puzzle and Richard would be the reader who submitted improve solutions.

    His sudoku solver is a brute force answer but works quite fast. What was interesting about it, however, is that he found that many published sudoku puzzles have multiple solutions! Since SQL is a set-oriented language, his answer attempted to produce all of the possible solutions.

    https://www.sqlservercentral.com/articles/a-sudoku-solver-for-ordinary-folk-1

     

     

    Please post DDL and follow ANSI/ISO standards when asking for help. 

  • WITH N AS(SELECT*FROM(VALUES('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'))s(n))
    ,S AS(SELECT'000071006002400810900600030007000600800000004005000300090004002051003900400950000's,1i
    UNION ALL SELECT LEFT(stuff(s,i,1,n),81),patindex('%0%',stuff(s,i,1,n))FROM S,N WHERE i>0 AND
    NOT EXISTS(SELECT 1FROM N WHERE M.n=substring(s,(i-1)%9/3*3+(i-1)/27*27+(n-1)/3*6+n,1)
    OR M.n=substring(s,(i-1)/9*9+n,1)
    OR M.n=substring(s,(i-1)%9+n*9-8,1)))
    SELECT*FROM S WHERE i=0

    As said, the minimum solution in characters, removal of the unnecessary white space, will give a single line with 461 characters.

    Short 'description', the puzzle is given in the string of 81 positions, the fields with a zero are 'empty' fields. On the same line, the value before the i is the position of the first zero in the string.

    As said, the minimum solution in characters (in SQL), not build for speed, but it's pretty speedy also. Not build for elegance, but I must say it is fairly elegant. The motivation was to showoff SQL as a very potent tool and to showoff that set based tools can be very power full. Here the solution is 'data' driven, not procedural driven.

    Ben

    later on I'll post a more extensive description. If the solution is allready self explaining for some, this would be nice.

     

  • After 'solving' this puzzle, I looked on line for other solves. Found :

    https://is.gd/Sudoku_shortest_solver

    The shortest on that site mentioned is 101 bytes K program. (In C the shortest is 174 bytes). Still had fun solving the puzzle in SQL, but I did not come close the the solutions on the above site.

    Ben

     

     

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply