Home More Samples
```///////////////////////////////////////////////////////////////////////////////
//
// Title: Building Blocks
// Author: Humphrey Dudley
// Publication: Dell Logic Puzzles
// Issue: April, 1998
// Page: 19 (top)
// Stars: 3
//
// Each of four alphabet blocks has a single letter of the alphabet on each of
// its six sides. In all, the four blocks contain every letter but Q and Z.
// By arranging the blocks in various ways, you can spell all of the words
// listed below.
// Can you figure out how the letters are arranged on the four blocks?
//
// BAKE ONYX ECHO OVAL
//
//
// LUCK VINY LUSH WRAP
//
///////////////////////////////////////////////////////////////////////////////
//
// Query:
//          all BuildingBlocks()
//
// Solution:
//
//          Block 0: B F I O U W
//          Block 1: A C D J N S
//          Block 2: H K M R V X
//          Block 3: E G L P T Y
//
///////////////////////////////////////////////////////////////////////////////

Block = [0..5]->I["A".."Z"]
Blocks = [0..3]->Block

pred BuildingBlocks() iff
blocks::Blocks & InitializeAllBlocks(blocks,0) &

// By assigning the letters "B","A","K","E" to the blocks 0,1,2,3 we
// will eliminate multiple equivalent solutions.
blocks(0,_) = "B" & blocks(1,_) = "A" & blocks(2,_) = "K" & blocks(3,_) = "E" &

// Place all constraints

CanSpell(blocks,"B","A","K","E") &
CanSpell(blocks,"O","N","Y","X") &
CanSpell(blocks,"E","C","H","O") &
CanSpell(blocks,"O","V","A","L") &
CanSpell(blocks,"G","I","R","D") &
CanSpell(blocks,"S","M","U","G") &
CanSpell(blocks,"J","U","M","P") &
CanSpell(blocks,"T","O","R","N") &
CanSpell(blocks,"L","U","C","K") &
CanSpell(blocks,"V","I","N","Y") &
CanSpell(blocks,"L","U","S","H") &
CanSpell(blocks,"W","R","A","P") &
// Print the solution in a friendly format.
PrintAllBlocks(blocks,0)

///////////////////////////////////////////////////////////////////////////////
// Setup problem constraints.
// Place four letters c1,c2,c3,c4 on four different blocks x,y,z,v.
// The letter placed on one block cannot appear on the other three blocks.
local pred CanSpell(blocks::Blocks,c1:<I,c2:<I,c3:<I,c4:<I) iff
blocks(x,_) = c1 & ~c1 in blocks(y) & ~c1 in blocks(z) & ~c1 in blocks(v) &
blocks(y,_) = c2 & ~c2 in blocks(x) & ~c2 in blocks(z) & ~c2 in blocks(v) &
blocks(z,_) = c3 & ~c3 in blocks(x) & ~c3 in blocks(y) & ~c3 in blocks(v) &
blocks(v,_) = c4 & ~c4 in blocks(x) & ~c4 in blocks(y) & ~c4 in blocks(z) &

// Insist none of the four indeces x,y,z,v are the same
x <> y & x <> z & x <> v & y <> z & y <> v & z <> v

///////////////////////////////////////////////////////////////////////////////
// Remove letters "Z" and "Q" from all blocks.
// Alphabetically sort each block.
local pred InitializeAllBlocks(blocks::Blocks,i:<I) iff
if i < Len(blocks) then
~"Q" in blocks(i) &
~"Z" in blocks(i) &
InitializeOneBlock(blocks(i),0) &
InitializeAllBlocks(blocks,i+1)
end

///////////////////////////////////////////////////////////////////////////////
// Ensure the elements of each block are in ascending order.
// This will eliminate all permutations of the elements, thus eliminating
// multiple equivalent solutions.
// This also ensures all elements on one block are different.
// Sorting ASCII characters in ascending order is equivalent to sorting
// alphabetically.
local pred InitializeOneBlock(block::Block,i:<I) iff
if i < Len(block) then
if i > 0 then
block(i) > block(i-1)
end &
InitializeOneBlock(block,i+1)
end

///////////////////////////////////////////////////////////////////////////////
// Print the solution
// Convert each numerical ASCII value into a string.
local proc PrintAllBlocks(blocks:<Blocks,i:<I) iff
if i < Len(blocks) then
Print('\nBlock ',i,': ') &
Print(blocks(i,0):S,' ',blocks(i,1):S,' ',blocks(i,2):S,' ',
blocks(i,3):S,' ',blocks(i,4):S,' ',blocks(i,5):S) &
PrintAllBlocks(blocks,i+1)
end

```