This answer produces the same four solutions mentioned at the end of kaine's answer, although the solution technique I used is a little less subtle, and more along the lines of brute force. It illustrates a technique that's ok for small problems like this, but probably not for larger problems. It consists of generating and testing all permutations of cell colors, and reporting those that satisfy all tests. It takes about 0.2 seconds to complete. Here is what the program output looks like:
yellow red green orange black purple white pink blue
yellow red green orange black purple pink white blue
green purple white pink blue black yellow red orange
green black red purple yellow orange pink white blue
Edit: In light of Rafe's comment that adjacency includes diagonal neighbors [eg, all other cells are adjacent to the center cell], in nextTo
I replaced two lines. I also corrected two program errors and have added some program explanation. Here's the current program's output (which matches kaine's first six solutions):
green purple white pink black blue yellow orange red
green orange white pink black blue yellow purple red
green purple white pink blue black yellow orange red
green purple white pink blue black yellow red orange
green orange white pink blue black yellow purple red
green orange white pink blue black yellow red purple
Following is the python 2.7 program that produced the output. I used the letters B, G, K, L, O, P, R, W, Y to stand for the nine colors Black, Green, pinK, bLue, Orange, Purple, Red, White, Yellow. The program uses cell identification numbers 0,1,2 for cells in the top row; 6,7,8 for the middle row; and 12,13,14 for the bottom row. Array cellNums
embodies this map, which is such that two cells in the same column have identification numbers that differ by $\pm6$ or $\pm12$.
For adjacency, two cells adjacent in a row differ by $\pm1$; in a column, by $\pm6$; in a forward diagonal, by $\pm5$; in a back diagonal, by $\pm7$. Thus, cells x
and y
are Manhattan adjacent, or are in a Von Neumann neighborhood, if $|x-y| \in \{1, 6\}$; and are in a Moore neighborhood if $|x-y| \in \{1, 6, 5, 7\}$.
Array entry digCells[i]
tells the cell number for digit i+1
. Array entry number[j]
tells what digit is in cell j
.
# Ref http://puzzling.stackexchange.com/questions/2194/color-the-table
from itertools import permutations, ifilter
#-----------------------------------------------------------
def inColumn(x,y): # Return true if x,y are in same column.
dxy = abs(x-y)
return dxy == 6 or dxy == 12
#-----------------------------------------------------------
def nextTo(x,y): # Return true if x,y are adjacent.
#return abs(x-y) in (1, 6) # von Neumann
return abs(x-y) in (1, 6, 5, 7) # Moore
#-----------------------------------------------------------
# Test if permutation p satisfies all conditions.
def isGood(p):
B, G, K, L, O, P, R, W, Y = p
# Tests 1 and 2.
# Note, digit 3 is in cell 1 and digit 5 is in cell 0
# 1. nextTo(Y,O); not nextTo(R, B)
# 2. color(number(3)) in {W,G,L,R}; color(number(5)) not in {W,P};
if nextTo(R, B) or not nextTo(Y,O) or \
1 not in (W,G,L,R) or 0 in (W,P):
return False
# Tests 3 and 4.
# 3. inColumn(P,O); not inColumn(Y,B); not inColumn(G,L)
# 4. abs(number(W)-number(K)) == 1; not nextTo(W,K)
if not inColumn(P,O) or inColumn(Y,B) or inColumn(G,L) or \
abs(number[W]-number[K]) != 1 or nextTo(W,K):
return False
# Tests 5 and 6.
# 5. abs(number(R)-number(Y)) < 3; abs(number(W)-number(L)) < 4
# 6. abs(number(R)-number(L)) > 2
if abs(number[R]-number[Y]) > 2 or abs(number[W]-number[L]) > 3 \
or abs(number[R]-number[L]) < 3:
return False
# Tests 7, 8, 9.
# 7. nextTo(G,K); odd(number(G))
# 8. not inColumn(R,Y)
# 9. number(L) > number(G)
if not nextTo(G,K) or not number[G]&1 or \
inColumn(R,Y) or \
number[L] <= number[G]:
return False
return True
#---------------------------Main program--------------------
cellNums = [0, 1, 2, 6, 7, 8, 12, 13, 14]
digCells = [13, 14, 1, 12, 0, 6, 7, 2, 8]
number = [5,3,8,0,0,0, 6,7,9,0,0,0, 4,1,2,0,0,0]
colors = ['black', 'green', 'pink', 'blue', 'orange', 'purple', 'red', 'white', 'yellow']
for p in ifilter(isGood, permutations(cellNums)):
for cn in digCells:
for j, c in enumerate(p):
if c==cn: print colors[j],
print