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
A SysAdmin's Guide to Change Management
 In the first in a series of monthly articles, ‘Confessions of a Sys Admin’, Matt describes the issues... Read more...

Exchange: Recovery Storage Groups
 It can happen at any time: You get a request, as Admin, from your company, to provide the contents of... Read more...

Build Your Own Virtualized Test Lab
 Desmond Lee explains the fundamentals of building a fully functional test lab for Windows Servers and... Read more...

Rendering Hierarchical Data with the Treeview
 It sometimes happens that Web Server controls that visualize data don't quite fit with the way that... Read more...

SQL Server 2008: Performance Data Collector
 With Performance Data Collector in SQL Server 2008, you can now store performance data from a number of... Read more...