Eight Queens

From John Senner
Revision as of 23:37, 15 May 2024 by John (talk | contribs) (Created page with "== SQL Eight Queens in SQL == '''Create the board:''' <source lang="sql"> CREATE TABLE rows ( id integer PRIMARY KEY ); INSERT INTO rows (id) VALUES (1),(2),(3),(4),(5),(6)...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

SQL Eight Queens in SQL

Create the board: <source lang="sql"> CREATE TABLE rows (

 id integer PRIMARY KEY

); INSERT INTO rows (id) VALUES (1),(2),(3),(4),(5),(6),(7),(8);

CREATE TABLE cols (

 id integer PRIMARY KEY

); INSERT INTO cols (id) VALUES (1),(2),(3),(4),(5),(6),(7),(8); </source>

Get a set of queens: <source lang="sql"> SELECT

 cols.id AS col1, rows.id AS row1,
 col2, row2,
 col3, row3,
 col4, row4,
 col5, row5,
 col6, row6,
 col7, row7,
 col8, row8

FROM rows, cols, (SELECT

 col3, row3,
 col4, row4,
 col5, row5,
 col6, row6,
 col7, row7,
 col8, row8,
 rows.id AS row2, cols.id AS col2
 FROM rows, cols, (SELECT
   col4, row4,
   col5, row5,
   col6, row6,
   col7, row7,
   col8, row8,
   rows.id AS row3, cols.id AS col3
   FROM rows, cols, (SELECT
     col5, row5,
     col6, row6,
     col7, row7,
     col8, row8,
     rows.id AS row4, cols.id AS col4
     FROM rows, cols, (SELECT
       col6, row6,
       col7, row7,
       col8, row8,
       rows.id AS row5, cols.id AS col5
       FROM rows, cols, (SELECT
         col7, row7,
         col8, row8,
         rows.id AS row6, cols.id AS col6
         FROM rows, cols, (SELECT
           col8, row8,
           rows.id AS row7, cols.id AS col7
           FROM rows, cols, (SELECT
             rows.id AS row8, cols.id AS col8
             FROM rows, cols)
           AS b8
           WHERE cols.id != col8 AND rows.id != row8
           AND (cols.id + rows.id != col8 + row8)
           AND (cols.id - rows.id != col8 - row8)
           ) AS b7
         WHERE cols.id != col8 AND rows.id != row8
         AND cols.id != col7 AND rows.id != row7
         AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
         AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
         ) AS b6
       WHERE cols.id != col8 AND rows.id != row8
       AND cols.id != col7 AND rows.id != row7
       AND cols.id != col6 AND rows.id != row6
       AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
       AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
       AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
       ) AS b5
     WHERE cols.id != col8 AND rows.id != row8
     AND cols.id != col7 AND rows.id != row7
     AND cols.id != col6 AND rows.id != row6
     AND cols.id != col5 AND rows.id != row5
     AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
     AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
     AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
     AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
     ) AS b4
   WHERE cols.id != col8 AND rows.id != row8
   AND cols.id != col7 AND rows.id != row7
   AND cols.id != col6 AND rows.id != row6
   AND cols.id != col5 AND rows.id != row5
   AND cols.id != col4 AND rows.id != row4
   AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
   AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
   AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
   AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
   AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
   ) AS b3
 WHERE cols.id != col8 AND rows.id != row8
 AND cols.id != col7 AND rows.id != row7
 AND cols.id != col6 AND rows.id != row6
 AND cols.id != col5 AND rows.id != row5
 AND cols.id != col4 AND rows.id != row4
 AND cols.id != col3 AND rows.id != row3
 AND cols.id != col2 AND rows.id != row2
 AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
 AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
 AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
 AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
 AND (cols.id + rows.id != col2 + row2) AND (cols.id - rows.id != col2 - row2)

LIMIT 1337; --Arbitrary, you can let yours go all day. </source>

Commentary: I realize that this could be more elegant by trimming out the hard-coded values, and that I could set it up for N queens, but I got excited when it ran for 8. I wrote a nonrecursive brute-force version that ended as expected, with me sighing and restarting Postgres. If I go and edit it, it'll certainly be to put the results in a human-readable form. Because it's really cool, but isn't smart enough to choose good placements ahead of time, I give myself 7 Queens out of a possible 8. Also, update since 2007, ChatGPT will just do this for you.