# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1541: Box clever

From New Scientist #2704, 18th April 2009 [link]

Put a digit in each of the following boxes so that (counting all the occurrences of digits in this Enigma from here until the final question) the following statements are true.

The number of occurrences of the digit 0 is ____

The number of occurrences of the digit 1 is ____

The number of occurrences of the digit 2 is ____

The number of occurrences of the digit 3 is ____

The number of occurrences of the digit 4 is ____

The number of occurrences of the digit 5 is ____

The number of occurrences of the digit 6 is ____

The total number of occurrences of the digits 7, 8 and 9 is ____

The average of the previous boxes is ____

The highest of the number of occurrences of 0, 1, 2 and 3 is ____

Please list, in order, the digits in the boxes.

[enigma1541]

### 2 responses to “Enigma 1541: Box clever”

1. Jim Randell 15 April 2012 at 5:29 pm

Here’s my original Perl solution. It runs in 1.9s.

```use strict;

use Enigma qw/MAX/;

our (\$B0, \$B1, \$B2, \$B3, \$B4, \$B5, \$B6, \$B789, \$BA, \$BM);

sub check {

my %d = (
0 => 2,
1 => 2,
2 => 2,
3 => 2,
4 => 1,
5 => 1,
6 => 1,
7 => 1,
8 => 1,
9 => 1,
);

map { \$d{\$_}++ } split '', "\$B0\$B1\$B2\$B3\$B4\$B5\$B6\$B789\$BA\$BM";

return unless \$B0 == \$d{0};
return unless \$B1 == \$d{1};
return unless \$B2 == \$d{2};
return unless \$B3 == \$d{3};
return unless \$B4 == \$d{4};
return unless \$B5 == \$d{5};
return unless \$B6 == \$d{6};

my \$d789 = \$d{7} + \$d{8} + \$d{9};
return unless \$B789 == \$d789;

my \$a = (\$d{0} + \$d{1} + \$d{2} + \$d{3} + \$d{4} + \$d{5} + \$d{6} + \$d789) / 8;
return unless \$BA == \$a;

my \$m = MAX(\$d{0}, \$d{1}, \$d{2}, \$d{3});
return unless \$BM == \$m;

# found a solution
print "0=\$B0:\$d{0} 1=\$B1:\$d{1} 2=\$B2:\$d{2} 3=\$B3:\$d{3} 4=\$B4:\$d{4} 5=\$B5:\$d{5} 6=\$B6:\$d{6} 7+8+9=\$B789:\$d789 avg=\$BA:\$a max=\$BM:\$m\n";
}

for \$B0 (2) {
for \$B1 (2..5) {
for \$B2 (3..8) {
for \$B3 (2..9) {
for \$B4 (1..9) {
for \$B5 (1..9) {
for \$B6 (1..9) {
for \$B789 (3..9) {
\$BA = (\$B0 + \$B1 + \$B2 + \$B3 + \$B4 + \$B5 + \$B6 + \$B789);
next unless \$BA % 8 == 0;
\$BA /= 8;
\$BM = MAX(\$B0, \$B1, \$B2, \$B3);
check();
}
}
}
}
}
}
}
}
```

Solution: The digits in the boxes are 2, 3, 4, 6, 2, 1, 3, 3, 3, 6.

• Jim Randell 15 April 2012 at 5:33 pm

And here’s my solution in Python. It runs in 54ms.

```from enigma import printf

# it will be impossible to enter 0 in any of the boxes
b0 = 2

# there are 24 digits in all (when the boxes are filled out),
# so the first 8 boxes must sum to that.
# hence their average is 24 / 8 = 3
ba = 3

# there can't be more than 5 1's
for b1 in range(2, 6):
s1 = b1 + b0
for b2 in range(3, 9):
s2 = b2 + s1
for b3 in range(2, min(10, 19 - s2)):
s3 = b3 + s2
for b4 in range(1, min(10, 20 - s3)):
s4 = b4 + s3
for b5 in range(1, min(10, 21 - s4)):
s5 = b5 + s4
for b6 in range(max(1, 15 - s5), min(10, 22 - s5)):
b789 = 24 - (b6 + s5)
bm = max(b0, b1, b2, b3)

# numbers of digits with empty boxes
#     0  1  2  3  4  5  6  7  8  9
d = [ 2, 2, 2, 2, 1, 1, 1, 1, 1, 1 ]

# add in the digits in the boxes
for n in (b0, b1, b2, b3, b4, b5, b6, b789, ba, bm):
d[n] += 1

# check the conditions
if d[0:7] != [b0, b1, b2, b3, b4, b5, b6]: continue
if sum(d[7:10]) != b789: continue
if max(d[0:4]) != bm: continue

printf("b0={b0} b1={b1} b2={b2} b3={b3} b4={b4} b5={b5} b6={b6} b789={b789} ba={ba} bm={bm}")
```