And for a mental break after a few weeks of living swamped-in-code, here’s One Tough Puzzle.

I spent a good deal of time writing programs to solve puzzles – in Java and PROLOG – for my AI Bachelor’s, and the last several solving problems that are harder and more useful to solve – in php and SQL. So it’s come to this, I’m using today’s tools to solve yesterday’s non-problems.

Here’s a hard-coded SQL script to solve this puzzle. It’s not terrifically extensible beyond 9 glossy pieces, but it is proof that SQL can do it given the chance.

CREATE TABLE tiles (id serial, one text, two text, three text, four text);

INSERT INTO tiles (one, two, three, four) VALUES

(‘s’,’s’,’h’,’c’),
(‘h’,’d’,’d’,’h’),
(‘s’,’d’,’h’,’d’),
(‘s’,’d’,’s’,’h’),
(‘c’,’h’,’d’,’c’),
(‘h’,’d’,’c’,’c’),
(‘h’,’s’,’s’,’c’),
(‘d’,’c’,’c’,’d’),
(‘c’,’h’,’s’,’h’);

SELECT mi.id as middle,
e1.id as cw1,
d1.id as cw2,
e2.id as cw3,
d2.id as cw4,
e3.id as cw5,
d3.id as cw6,
e4.id as cw7,
d4.id as cw8

FROM tiles AS mi

JOIN tiles AS e1 ON e1.id not in (mi.id) AND ((mi.one = e1.three) OR (mi.one = e1.four))
JOIN tiles AS e2 ON e2.id not in (mi.id, e1.id) AND ((mi.two = e2.three) OR (mi.two = e2.four))
JOIN tiles AS e3 ON e3.id not in (mi.id, e1.id, e2.id) AND ((mi.three = e3.one) OR (mi.three = e3.two))
JOIN tiles AS e4 ON e4.id not in (mi.id, e1.id, e2.id, e3.id) AND ((mi.four = e4.one) OR (mi.four = e4.two))

JOIN tiles AS d1 ON d1.id not in (mi.id, e1.id, e2.id, e3.id, e4.id) AND
(((e1.three = d1.one) AND (d1.four = e2.one)) OR
((e1.three = d1.two) AND (d1.one = e2.four)) OR
((e1.two = d1.three) AND (d1.two = e2.four)) OR
((e1.two = d1.four) AND (d1.three = e2.one)))

JOIN tiles AS d2 ON d2.id not in (mi.id, e1.id, e2.id, e3.id, e4.id, d1.id) AND
(((e2.three = d2.one) AND (d2.four = e3.two)) OR
((e2.three = d2.two) AND (d2.one = e3.three)) OR
((e2.two = d2.three) AND (d2.two = e3.three)) OR
((e2.two = d2.four) AND (d2.three = e3.two)))

JOIN tiles AS d3 ON d3.id not in (mi.id, e1.id, e2.id, e3.id, e4.id, d1.id, d2.id) AND
(((e3.four = d3.one) AND (d3.four = e4.two)) OR
((e3.four = d3.two) AND (d3.one = e4.three)) OR
((e3.one = d3.three) AND (d3.two = e4.three)) OR
((e3.one = d3.four) AND (d3.three = e4.two)))

JOIN tiles AS d4 ON d4.id not in (mi.id, e1.id, e2.id, e3.id, e4.id, d1.id, d2.id, d3.id) AND
(((e4.four = d4.one) AND (d4.four = e1.one)) OR
((e4.four = d4.two) AND (d4.one = e1.four)) OR
((e4.one = d4.three) AND (d4.two = e1.four)) OR
((e4.one = d4.four) AND (d4.three = e1.one)))

ORDER BY middle DESC;