# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1612: So logical

From New Scientist #2777, 11th September 2010 [link]

Joe has 16 small square cards, eight black and eight white. On each card is one of the numbers 1 2 3 or 4. There is a 1 on four white cards (W1), a 1 on six black cards (B1), a 2 on two white cards (W2), a 2 on two black cards (B2), a 3 on one white card (W3) and a 4 on one white card (W4). All Penny has to do is place the cards on the grid (above right) with a 1 on a black card in the top right corner, so that the number on each card indicates how many black cards are immediately adjacent to it horizontally and vertically.

What cards did Penny place on A B C and D?

[enigma1612]

### One response to “Enigma 1612: So logical”

1. jimrandell 30 December 2011 at 9:41 am

The following Python program tries all combinations of black and white cards in the remaining 15 squares, and runs in an acceptable time of 385ms.

```# consider the square:
#  3  2  1  0
#  7  6  5  4
# 11 10  9  8
# 15 14 13 12

from itertools import product

CARDS = { 'W1': 4, 'W2': 2, 'W3': 1, 'W4': 1, 'B1': 6, 'B2': 2 }

[ 1, 4 ],
[ 0, 2, 5 ],
[ 1, 3, 6 ],
[ 2, 7 ],
[ 0, 5, 8 ],
[ 1, 4, 6, 9 ],
[ 2, 5, 7, 10 ],
[ 3, 6, 11 ],
[ 4, 9, 12 ],
[ 5, 8, 10, 13 ],
[ 6, 9, 11, 14 ],
[ 7, 10, 15 ],
[ 8, 13 ],
[ 9, 12, 14 ],
[ 10, 13, 15 ],
[ 11, 14 ]
]

def check(grid):
cards = dict((x, 0) for x in CARDS.keys())
for i in range(len(grid)):
# count the number of adjacent black cells
n = [grid[x] for x in adjacent[i]].count('B')
if i == 0 and n != 1: return
card = grid[i] + str(n)
if card not in cards: return
cards[card] += 1
if cards != CARDS: return
print(' '.join(grid), \
'=>', \
' '.join((grid[x] + str([grid[y] for y in adjacent[x]].count('B')) for x in (3, 6, 9, 12))))

# generate B/W combinations for 15 squares
for x in product(('W', 'B'), repeat=15):
check(['B'] + list(x))
```

Solution: A=W1, B=B1, C=B1, D=W1.