Enigmatic Code

Programming Enigma Puzzles

Enigma 402: A DIY puzzle

From New Scientist #1552, 19th March 1987 [link]

In the puzzle below, select some of the guest lists and discard the remainder. The lists you keep should make a puzzle which has exactly one solution and involves no more lists than is necessary.

Who did it?

by …

There has been a series of robberies at house parties recently. Each was clearly a one-man job — the same man each time — and each was an inside job. The possible suspects are Alan, Bryan, Chris, David, Eric, Fred, George, Harry, Ian, Jack, Ken and Len. The male guest list at the parties were as follows:

1. All but David, George and Len.
2. Bryan, Chris, Eric, Harry, Ian and Ken.
3. All but Bryan and Ken.
4. Chris and Ian.
5. All but Alan, Fred, Jack and Len.
6. Bryan, Chris, Ian and Ken.
7. All but Eric and Harry.
8. All but David and George.
9. All but Chris and Ian.
10. All but Alan and Fred.

Who carried out the robberies?

Which lists should you use in your puzzle?

What is the answer to your puzzle?

[enigma402]

Puzzle 71: All wrong, all wrong

From New Scientist #1122, 28th September 1978 [link]

A couple of one’s, a couple of two’s and a six;
All wrong, all wrong!

If only I thought that the puzzle was one I could fix,
I’d sing a song.

But as I feel sure that it’s rather too much for me,
My voice is muted.

Uncle Bungle’s my name and I fear that you must agree,
I’m rather stupid.

So please, I implore,
Continue the fight,
With tooth and with claw,
With main and with might,
To make wrong sums right.

puzzle-71

The figures given are all incorrect. Write out the whole division sum.

[puzzle71]

Enigma 1110: Dots and lines

From New Scientist #2266, 25th November 2000

Matthew and Ben are playing a game. The board is a 1-kilometre square divided into 1-centimetre squares. The centre of each small square is marked by a red dot.

Matthew begins the game by choosing a number. Ben then selects that number of red dots. Finally Matthew chooses two of Ben’s selected dots and draws a straight line from one to the other. Matthew wins if his line passes through a red dot other than those at its ends; otherwise Ben wins.

What is the smallest number that Matthew can choose to be certain of winning?

In the magazine this puzzle was incorrectly labelled Enigma 1104.

[enigma1110]

Enigma 400: Potential difficulties

From New Scientist #1550, 5th March 1987 [link]

I asked Electrophorus what he was working on.

“You know that joining unlike terminals of a pair of batteries produces a voltage across the two free terminals equal to the sum of the voltages of the separate batteries. And connecting unlike terminals produces a voltage equal to the difference of the voltages of the separate batteries?”

“Yes”, I replied. “With a battery of 2 volts and one of 5 volts one obtains 3 volts (sources opposing) or 7 volts (sources reinforcing).”

“Well, before lunch I had three batteries, none of which had zero voltage, and a voltmeter with a holder that would accommodate only two batteries. So I measured the voltages across the free terminals of all possible pairwise combination of these three batteries both in the case where the voltages reinforced and where they opposed. I wrote on my blackboard the resulting six positive numbers in order of increasing magnitude.”

“When I returned from lunch eager to calculate the ratings of the three batteries, I found the three batteries gone and my blackboard wiped clean. I remember that the second smallest reading occurred twice. It was either 13 or 17 volts, I forget which. I had noticed, rather inconsequentially perhaps, that reversing the digits of this double reading produced another reading which occurred in my measurements.”

What were the ratings of the three batteries?

[enigma400]

Tantalizer 479: Cat and five tales

From New Scientist #1030, 9th December 1976 [link]

Someone let the cat out. Who was it? That is rather hard to decide. Delia says it was one of the twins, meaning Bert or Claud. Alice says it was Bert; and Bert (shame on him!) says it was Claud. Meanwhile Claud says it was Delia; and Emma says it was not Claud.

So it is all a bit of a puzzle and you will be expecting to be told how many of them are right in what they say. But that would make it all much too easy, as you could then deduce who the culprit was. So you will just have to manage with what information you have.

Who let the cat out?

[tantalizer479]

Enigma 1111: Base-age

From New Scientist #2267, 2nd December 2000

Fill in the following cross-figure. No answer begins with a zero. The same base is used for all the entries, but it is not necessarily 10.

Across
1. A palindromic prime.
4. The square of the base being used.
5. A square.

Down
1. Three times my son’s age.
2. A prime.
3. A palindromic square.

How old is my son?

[enigma1111]

Enigma 399: Time, gentlemen, please

From New Scientist #1549, 26th February 1987 [link]

The beer-mats at our local pub have puzzles on them. Here is one in which the digits are consistently replaced by letters.

BEER – MAT = TEST
NINE is a perfect square
IT is a number
THIS is odd!

What is TIME gentlemen (and ladies) please?

[enigma399]

Tantalizer 480: Pitter patter

From New Scientist #1031, 16th December 1976 [link]

When the Olympic games were last held in Patagonia, the Famous torch entered the country at a point exactly 35.27 km from its pedestal in the Olympic stadium. The honour of transporting it from the frontier fell to two Patagonian athletes, Pita and Pata, who were to carry it in turns for the 35.27 km. By presidential decree each was to carry it at each turn any distance he pleased not less than 1 km and not more than 2 km.

Each secretly resolved that he would be the one to carry it the final awesome metre. Since there was nothing in the decree to forbid a different choice of distance at each turn much calculation went on before Pita and Pata tossed for the privilege of having the first turn. In fact Pita won the toss and chose second turn.

Did he chose right?

[tantalizer480]

Enigma 1112: Patio zones

From New Scientist #2268, 9th December 2000

George is building a patio, which will be covered using one-foot-square concrete slabs of seven different colours. He has divided the rectangular patio into seven rectangular zones, without any gaps.

Each zone will be covered by slabs of one colour, with five different colours appearing around the perimeter of the patio, and four different colours at the corners. The seven rectangular zones are all different shapes, but all have the same perimeter, which is less than 60 feet.

What are the dimensions of the patio that George is building?

This puzzle is referenced by Enigma 1221.

[enigma1112]

Enigma 398: Down on the farm

From New Scientist #1548, 19th February 1987 [link]

Farmer O. R. Midear has crossed a turnip with a mangel to get a tungel, and he now has many fields of tungels. As a tungel expert, he looks at a tungel to see if it is red or not, if it is smooth or not, and if it is firm or not. He knows that if a tungel is red then it is smooth.

Regulations have just been introduced which put fields of tungels into classes A, B, C, D, E; a field may be in more than one class.

Class A: All fields containing no red tungel.
Class B: All fields containing no smooth tungel.
Class C: All fields in which every red tungel is firm.
Class D: All fields in which every smooth tungel is firm.
Class E: All fields containing a red tungel which is not firm.

To test Farmer Midear’s understanding of the regulations he was asked to say which of the following statements are true.

1. If a field is in A then it is in B.
2. If a field is in B then it is in A.
3. If a field is in C then it is in B.
4. If a field is in B then it is in C.
5. If a field is in C then it is in D.
6. If a field is in D then it is in C.
7. If a field is in D then it is not in E.
8. If a field is not in E then it is in C.
9. In every field there is a tungel such that if it is not red then the field is in A.

What should Farmer Midear’s answer be?

[enigma398]

Puzzle 72: Addition: letters for digits

From New Scientist #1123, 5th October 1978 [link]

In the addition sum below, letters have been substituted for digits. The same latter stands for the same digit whenever it appears and different letters stand for different digits.

Write the sum out with numbers substituted for letters.

[puzzle72]

Enigma 1113: Ten + ten = twenty

From New Scientist #2269, 16th December 2000

In the multiplications shown, where the combined products of multiplications (I) and (II) (both identical) equal the product of multiplication (III), each letter consistently represents a specific digit, different letters being used for different digits while asterisks can be any digit.

The multiplications in fact are not difficult to solve, and easier still if I told you that TEN is even.

How much is TWENTY?

[enigma1113]

Enigma 397: All wrong again

From New Scientist #1547, 12th February 1987 [link]

In the following addition sum all the digits are wrong. But the same wrong digit stands for the same correct digit wherever it appears, and the same correct digit is always represented by the same wrong digit.

Find the correct addition sum.

[enigma397]

Tantalizer 481: Happy Christmas

From New Scientist #1032, 23rd December 1976 [link]

tantalizer-481

Oops! What the message is meant to say is of course:

HAPPY CHRISTMAS TO YOU FROM THE NEW SCIENTIST.

Perhaps you would like to put it right by sliding on word at a time along a line into a vacant oval. If you are not too saturated with Christmas pud, you should manage it in 26 moves.

[tantalizer481]

Enigma 1114: Christmas changes

From New Scientist #2270, 23rd December 2000

Christmas is said to change things and so this enigma is an old puzzle with some changes.

Problem: You have to assign a digit to each of the 10 letters in the sum here:

When you have decided on an assignment of digits to the 10 letters, then your assignment is a solution of the problem if it satisfies at least one of the following conditions:

• At least one digit is assigned to more than one letter.
• When the digits are put into the addition sum it is not correct.
• The digit assigned to “U” is smaller than the digit assigned to “H”.

You need to find an assignment of digits to the 10 letters which is NOT a solution of the problem.

What is the value of BLEAT in your assignment?

[enigma1114]

Bit Twiddling

Every so often I click on the Random Post link at the top right of the site, and have another look at a previously published problem. And sometimes I’ll think about attacking a problem in a different way than my original solution, and post an additional comment.

I recently revisited Enigma 69: Maximum Queen moves to see if I could think of a better algorithm to enable me to expand the analysis beyond a 5×5 board. As it happened I didn’t come up with a better algorithm, but I did refine the one I’d previously published to be more efficient. By eliminating symmetrically equivalent positions, using bit vectors to represent the board, and pre-computing moves I was able to get my program running about 150 times faster than my simple implementation. While this isn’t enough to rescue the pathologically slow nature of the algorithm as the size of the board increases, it does bring the solution for the 6×6 board into a feasible time frame.

After 14 hours of CPU time (less actual time, as we can run multiple copies of the program to consider different numbers of Queens) I was able to confirm that 128 moves was indeed maximal for the 6×6 board, with the Queens arranged in a “border” configuration (and I was able to do some analysis, but not an exhaustive treatment, of the 7×7, 8×8 and 9×9 boards).

As the board was now represented by a bit vector, I was looking for an efficient way to generate all k-bit patterns in an n-bit vector. And that was when I stumbled on the Bit Twiddling Hacks page. Right down at the end of the page is a section called “Compute the lexicographically next bit permutation”.

The page is primarily concerned with C (or C++) code, but the implementation in Python is as follows.

Suppose we start with the variable v holding an integer with k bits set when represented in binary. Then the following code fragment computes w, which is the next largest integer after v that also has k bits set in it’s binary representation.

t = (v | (v - 1)) + 1
w = t | ((((t & -t) // (v & -v)) >> 1) - 1)

So, if we want to generate all 8-bit numbers with exactly 2 bits set, we can simply start with the smallest number with 2 bits set (11 in binary, which is 3 in decimal), and keep applying the the code above until we exceed 255 (the largest 8-bit number).

v = 3
while v < 256:
  print(f"{v:08b} = {v}")
  t = (v | (v - 1)) + 1
  v = t | ((((t & -t) // (v & -v)) >> 1) - 1)

When run (under Python 3.6) we get the following result:

00000011 = 3
00000101 = 5
00000110 = 6
00001001 = 9
00001010 = 10
00001100 = 12
00010001 = 17
00010010 = 18
00010100 = 20
00011000 = 24
00100001 = 33
00100010 = 34
00100100 = 36
00101000 = 40
00110000 = 48
01000001 = 65
01000010 = 66
01000100 = 68
01001000 = 72
01010000 = 80
01100000 = 96
10000001 = 129
10000010 = 130
10000100 = 132
10001000 = 136
10010000 = 144
10100000 = 160
11000000 = 192

Which is a complete list of all 28 8-bit integers with exactly 2 bits set, in numerical order. And if we didn’t limit the numbers to 8-bits the program would continue generating integers with exactly 2 bits set indefinitely.

I might add this function to the enigma.py library (if I can think of a good name for it), but if you are trying to speed up a program it will probably be faster to just use the code fragment inline.

Enigma 396: The hostess’s problem

From New Scientist #1546, 5th February 1987 [link]

At a recent dinner party five men and their wives sat at the 10 places around the table. Men and women alternated around the table and no man sat next to his own wife. No man’s Christian name had the same initial as his surname.

Mrs Collins sat between Brian and David. Colin’s wife sat between Mr Briant and Mr Edwards. Mr Allen sat between Edward’s wife and Mrs Davidson. Brian’s wife sat next to Alan.

In the information which I’ve just given you, if two people were sitting next to each other then I have not told you about it more than once.

Which two men (Christian name and surname of each) sat next to Mrs Edwards?

[enigma396]

Tantalizer 482: Lapses from grace

From New Scientist #1033, 6th January 1977 [link]

An air of rare humility pervades the Common Room at St. Aletheia’s tonight. The seven inmates overdid the post-prandial gin and rashly confessed their sins to one another. Each owned to a different pair of the deadly ones and each sin turned out to have claimed a different pair of victims.

Constance, Emily and Flavia have no sin in common to any two of them. Beatrice, Deborah, Emily and Gertrude confessed to all seven between them. Alice and Gertrude admitted to sloth; Deborah and Emily to lust. Alice is not given to pride nor Beatrice to avarice nor Flavia to either pride or intemperance. Constance, who owned to anger, has a sin in common with Deborah, who did not.

Which pair has fallen prey to intemperance and which pair to envy?

[tantalizer482]

Enigma 1115: New Christmas star

From New Scientist #2270, 23rd December 2000

enigma-1115

Here is another “magical” Christmas star of twelve triangles, in which can be seen 
six lines of five triangles (two horizontal and two in each of the diagonal directions). Your task is to place a digit in each of the twelve triangles so that:

all six digits in the outermost “points” of the star are odd;

the total of the five digits in each line is the same,
 and it is the same as the total of the six digits in the points of the star;

each of the horizontal lines of digits, when read as a 5-digit number, is a perfect square.

What are those two perfect squares?

Thanks to Hugh Casement for providing the source for this puzzle.

[enigma1115]

Enigma 395: By Jove, it figures!

From New Scientist #1545, 29th January 1987 [link]

In the addition sum below, each of the digits from 0 to 9 has been replaced by a letter whenever it occurs. Different letters stand for different digits. You are asked to reproduce the original sum.

[enigma395]