Home > Retaining Water > Description

Retaining Water

The Water Retention Problem

A magic square is an n x n array of numbers consisting of the integers 1, 2, ..., n2 arranged so that the sum of the numbers in every row, column, and main diagonal is the same.

For example, in this 4 x 4 magic square the numbers 1, 2, ..., 16 are arranged so that each row, each column and both main diagonals add up to 34:


A magic square can be thought of as being able to "retain water". Think of each cell in the magic square as having a height equal to its value. A cell which is shorter than its 4 closest neighbors retains water up to the height of its shortest neighbor (and possibly higher -- see below). A cell which is at the outer edge of the magic square does not retain water.

For example, this magic square retains 3 units of water:

Figure A: A 4 x 4 magic square that retains 3 units of water

Sometimes adjacent cells, together, can retain more water than the sum of their individual water-retention capacities. For example, this magic square retains 5 units of water:

Figure B: A 4 x 4 magic square that retains 5 units of water

The water retention problem, for a given n, is to find the n x n magic square which retains the most water. (Thanks to Craig Knecht for calling this problem to my attention.)

The Contest

Submit (see How to Enter, below) your best solutions to the water retention problem for values of n from 4 to 28, inclusive. There are thus 25 individual problems which you are asked to solve.

You can submit more than one solution for the same problem, but if you do we count only your best solution. There is no penalty for submitting multiple solutions for the same problem.

See The Scoring System, below, to learn how we determine the winner.

The Prizes

First prize is your choice of any Bathsheba Grossman sculpture from one of these pages:

Second prize is your choice of any Bathsheba Grossman sculpture on one of these pages:

How to Enter

Just paste your magic squares into the large box on the Submit page and click the Submit Entry button. Format your magic squares as follows:

  • An individual magic square consists of a comma-delimited list of rows.
  • A row consists of a comma-delimited list of cell values, enclosed in parentheses.
  • Submit multiple magic squares in a single entry by separating them with semicolons. Do not put a semicolon after your last magic square.
  • Include spaces and line breaks anywhere you like (except within a number) to improve readability.

For example, to submit the first magic square from above you might enter: (16,3,2,13), (5,10,11,8), (9,6,7,12), (4,15,14,1)

To submit magic squares for n = 4 and n = 5 in a single entry, you could enter: (16,3,2,13), (5,10,11,8), (9,6,7,12), (4,15,14,1); (17,24,1,8,15), (23,5,7,14,16), (4,6,13,20,22), (10,12,19,21,3), (11,18,25,2,9)

Do not submit entries under more than one account. This is important. Do not submit entries under more than one account.

The Scoring System

We give a raw score to each solution you submit. The raw score is the quantity of water retained by the magic square in your solution. For example, figures A and B above would receive raw scores of 3 and 5, respectively.

Each time you submit a solution we will merge it with your prior solutions, if any. The result will be a virtual entry containing your best solutions for each of the 25 problems. We will give each of these 25 solutions a subscore from 0 to 1 and their sum will be your contest score.

We calculate subscores for the individual solutions as follows. If your solution has the highest raw score that was submitted for that problem, we give it 1 point; otherwise we give it only a fraction of a point. The fraction is the solution's raw score divided by the highest raw score submitted by anyone for that same problem.

Let's walk through a simplified example. Suppose that we reduce the contest to only 3 values of n: 4, 5 and 6.

Further suppose that we have 3 entrants (Holmes, Watson and Lestrade) and that their best solutions have raw scores as follows:

4 x 4 5 x 5 6 x 6
Holmes 5 10 13
Watson 2 13 17
Lestrade 4 8 18

We note the highest raw score for each problem, as follows:

4 x 4 5 x 5 6 x 6
Highest Raw Score 5 13 18

Finally, we compute the subscores and contest score for each entrant:

4 x 4 5 x 5 6 x 6 Contest Score
Holmes 5 / 5 = 1.0000 10 / 13 = 0.7692 13 / 18 = 0.7222 2.4914
Watson 2 / 5 = 0.4000 13 / 13 = 1.0000 17 / 18 = 0.9444 2.3444
Lestrade 4 / 5 = 0.8000 8 / 13 = 0.6154 18 / 18 = 1.0000 2.4154

If two entrants have the same contest score, we break the tie by giving preference to the entrant whose last improvement was submitted least recently.

Getting Your Questions Answered

First, check the FAQ section below. If you can't find the information you need there, send your question to the discussion group. If your question is of a personal nature, and not of general interest, send an email directly to Al Zimmermann.

The Discussion Group

If you think you might enter the contest, you should join the contest discussion group. You can join either by sending a blank email here or by visiting the group on groups.io. The discussion group serves two purposes. First, it allows contestants to ask for clarifications to the rules. Be aware that sometimes these requests result in changes to the rules, and the first place those changes are announced is in the discussion group. Second, the discussion group allows contestants to interact with each other regarding programming techniques, results and anything else relevant to the contest.

My Lawyer Would Want Me To Say This

I reserve the right to discontinue the contest at any time. I reserve the right to disqualify any entry or entrant for any reason that suits me. I reserve the right to interpret the rules as I see fit. I reserve the right to change the contest rules in mid-contest. In all matters contest-related, my word is final.

Frequently Asked Questions

Can I enter the contest more than once, using different accounts?

No. Submitting entries from more than one account is not permitted.

Can teams enter the contest?

Collaboration is allowed. However, only one of the collaborators may register. If two contestants are found to have collaborated, even if this occurred before one or both registered, both will be disqualified.

What information about my solutions can I share in the discussion group?

There are two types of information that you are forbidden to post. The first is specific solutions. The second is code. You may post scores, so if you want to tell everyone that you got a raw score of 99 for n = 20 (whether true or not), go right ahead. You may also discuss the algorithms you are using.

How can I find out what my individual subscores are?

You can't. I know this is frustrating, but it's a long standing policy that isn't going to change. Over the years it's been hotly debated in the discussion group and the contest administrator appears to have very strong feelings on the matter. You're going to have to learn to live with it. And I'd think twice before raising the issue yet again.

After I submit a solution, the scorer shows me the solution's "canonical representation". What is that?

After the scorer calculates your raw score it rotates and/or reflects the magic square in your solution to create a standard representation. If you build the magic square of the canonical representation, you'll see that it is equivalent to your original magic square. Having canonical versions of your solutions makes it easier to notice when two seemingly different solutions are fundamentally the same.