The magazine of the Melbourne PC User Group

The Five Square Puzzle
Ken Holmes

For program listing - download AGEPUZZ.bas


So, how many of you tried to type the program from "Solving puzzles with Basic" in the August issue? If so, we owe an apology for three missing "less than" signs (<) - a technical foible of the publishing program which has its own use for this symbol. I had no complaints so assume those interested worked it out. And, how many had a go at the Age-type puzzle mentioned at the end? I'm assuming you are a lazy lot to give me an excuse to cover it with this article.

Puzzle addicts are familiar with the type that appears in The Age each Saturday. A 5 x 5 table contains letters a - k, representing the digits 0 - 9, and the totals are given for all rows and columns. All ten digits must be used and two letters have their values already revealed. The central square is blank but has a value equal to one of the letters, which we must find; let us call it "x" initially.

I assume it is expected that most will try to solve it by arithmetical trial and error. The fact that no two letters can have the same value would lower the number of possibilities to be tried, but I have never persisted with this approach as I have better things to do on a Saturday. An algebraic route seems more interesting. Putting aside row 3 and column 3 for the moment, the other eight rows/columns provide eight relationships between the eight unknown variables - eminently solvable. Successive manipulations of chosen pairs of equations, in the way of all good algebrologists, will find the values eventually; it is wise to number the equations to keep track as you will end up with about thirty of them. Then you can use either row 3 or col 3 to get the value of "x" and hence the letter to which it is equal.

Using a computer program, in your language of choice, lets you take the trial and error approach as it will run speedily through the possibilities. The program is a nesting of FOR/NEXT loops and IF/END IF blocks which may need to be varied slightly depending on the positioning of the variables, particularly the two revealed values.

We start by selecting a row or column with the least number of unknowns - row 2 in the example shown. Nested loops for "g" and "b" get us started and, for each pair, "c" is determined by row 2. Comments are very useful in keeping track of which row/col you have used; also, it helps to mark in the puzzle table which letters you have already involved by putting a dot near them, ie. "g", "b" and "c" here, in row 2 and every other occurrence.

Note that we won't try to avoid false solutions where two letters have the same value or letters have values outside the range 0 - 9, as we can print out all solutions and ignore the obviously wrong ones. This avoids a lot of tedious programming and does not significantly affect running time.

Now we see that col 4 has only two unknowns, "j" and "e", so we loop through "j" and, for each value, "e" is given by col 4. Also, "k" will be given by col 5 and "d" by row 4. Next, we nest a couple of IF / END IF blocks to check that col 2 and row 1 are satisfied. If so, "f" will be determined using col 1 and, then, another IF / END IF block will check whether row 5 is satisfied. It is? We have a solution and can use row 3 (or col 3) to find "x".

Now we print out the letter values and, by inspection, can reject the wrong solutions and nominate which letter is equal to "x" in the correct one. The solution is in a comment in the middle of the program. It also prints out a "solution" which obviously doesn't meet all criteria, that is,
a b c d e f g h j k x
7 1 6 1 4 2 7 8 8 -2 6

When you have written this successfully (in QBasic), you can make a copy of it as a template to solve next Saturday Age's puzzle by changing the letters and numbers and, probably, the exact sequence of nestings. I like to think everyone is like me and makes silly mistakes - it never works the first time, programming keeps you humble. If you are really masochistic, try for a full program which won't let any wrong solutions through. For example, after FOR b = 0 TO 9, start an IF/END IF block as follows, IF b <> g THEN. Then, after finding "c", start IF c <> g AND c <> b AND c < 10 AND c > -1 THEN. Make sure the "END IF"s are placed for proper nesting. It gets (much) worse and you will probably give up.

Of course, you can translate all of this to your favourite language. After all, this is really about practicing programming, not solving puzzles.

Reprinted from the October 1998 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]