FUN only, solving a Puzzle.

  • ben.brugman

    SSChampion

    Points: 13350

    /*

    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

    */

  • ChrisM@Work

    SSC Guru

    Points: 186107

    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 months, 3 weeks ago by  ChrisM@Work.

    [font="Arial"]“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw[/font]


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

  • ben.brugman

    SSChampion

    Points: 13350

    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 months, 3 weeks ago by  ben.brugman. Reason: typo
  • jcelko212 32090

    SSCrazy Eights

    Points: 9010

    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. 

  • ben.brugman

    SSChampion

    Points: 13350

    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.

     

  • ben.brugman

    SSChampion

    Points: 13350

    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 6 (of 6 total)

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