Click here to monitor SSC

Inside the tent....

Occasional Editorial announcements.

Winners of the Bejewelled Puzzle

Published Monday, November 03, 2008 11:10 AM

There are three winners:

  • Timothy Walters - for his second solution with the offset table; He gets a $50 Amazon Voucher.
  • Ryan Randall – for his first and second solutions; He gets a choice between a license for SQL Prompt and SQL Data Generator
  • DrLechter – for his first and second solutions; He gets a choice between a license for SQL Prompt and SQL Data Generator

Timothy Walters's solution is very simple and elegant. Almost all of the logic has been implemented in the lookup table and that makes the solution portable and flexible.

In addition, his solution can be easiely modified in order to show the directions of the moves by adding one more column to the offset table.

Here is how it can be done:


--====== Table matches needs to be loaded only once
CREATE TABLE matches(offsetRow1 INT, offsetCol1 INT, offsetRow2 INT, ofsetCol2 INT, directions VARCHAR(20))
-- for horizontal
INSERT INTO matches VALUES(-1, -1, -1, -2, 'up')
INSERT INTO matches VALUES(-1, -1, -1, 1, 'up')
INSERT INTO matches VALUES(-1, 1, -1, 2, 'up')        
INSERT INTO matches VALUES( 1, -1, 1, -2, 'down')          
INSERT INTO matches VALUES( 1, -1, 1, 1, 'down')
INSERT INTO matches VALUES( 1, 1, 1, 2, 'down')      
INSERT INTO matches VALUES( 0, -2, 0, -3, 'left')    
INSERT INTO matches VALUES( 0, 2, 0, 3, 'right')            
-- for verical
INSERT INTO matches VALUES(-2, -1, -1, -1, 'left')
INSERT INTO matches VALUES(-1, -1, 1, -1, 'left')
INSERT INTO matches VALUES( 1, -1, 2, -1, 'left')
INSERT INTO matches VALUES(-2, 1, -1, 1, 'right')
INSERT INTO matches VALUES(-1, 1, 1, 1, 'right')
INSERT INTO matches VALUES( 1, 1, 2, 1, 'right')
INSERT INTO matches VALUES(-2, 0, -3, 0, 'up')
INSERT INTO matches VALUES( 2, 0, 3, 0, 'down')

--==================================================
;WITH CTE
      
AS
  
(
  
SELECT
        
[Row] = CAST( [#] AS INT ),
        
[Col] = CAST( [Col] AS INT ),
        
[Value]
    
FROM bejeweled
        UNPIVOT
([Value] FOR [Col] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])) unpvt
  
)
SELECT DISTINCT T.Row, T.Col, T.Value, directions
  
FROM CTE T
      
JOIN CTE T1
      
ON T.Value = T1.Value
      
JOIN CTE T2
      
ON T.Value = T2.Value
      
JOIN matches
      
ON (T1.Row - T.Row) = offsetRow1
    
AND (T1.Col - T.Col) = offsetCol1
    
AND (T2.Row - T.Row) = offsetRow2
    
AND (T2.Col - T.Col) = ofsetCol2
  
ORDER BY T.Row, T.Col

There is one more person – AmandaS that should be specially mentioned. Her second solution was very similar to Timothy Walters' and Ryan Randall's solutions, but was submitted five days later.

A few more people should be mentioned as well:

  • Saharafrog – interesting solution
  • Doug Stewart
  • Steve Munson - his solution shows the coordinates of the cells that should be moved and the coordinates of destination cells
  • RBarry Young
  • Dennis A
  • SkyBeaver

Comments

No Comments
You need to sign in to comment on this blog
<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...