Let the characters 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ represent the particles of 36 immiscible fluids, each more dense than the last. So the 0 fluid is the least dense and floats on all the others, while Z is the densest and always sinks.

Let # represent a solid particle that cannot move at all..

Consider a rectangular grid of some of these 37 characters, with anything out of bounds treated as #. Gravity will want to stabilize them with the least dense fluids on top and the most dense on the bottom.

Our procedure for stabilization is:

  1. Choose a random grid cell c that is not a # and has a density greater than that of at least one of the 3 neighboring cells on the layer below it (southwest, south, and southeast) that is not a #.
    Stop procedure if no such choice exists.
  2. Let d be a cell randomly sampled from these valid neighboring cells with the least densities.
  3. Swap the values at c and d.
  4. Repeat procedure until it has stopped.


Running the procedure on


might start out step 1 by selecting the A. There is only one cell below it with smaller density so the next grid would be


The procedure might continue on as

101 100 000 000
#00 #01 #11 #01
A20 A20 A20 A21

The last arrangement being stable.


Write a program that takes in a rectangular string of the 37 characters via stdin, command line, or function call. Initially, the given grid should be printed to stdout, but each time Enter is hit, one swap (step 3) should be done and the grid reprinted, separated from the last grid by an empty line. When the grid with no more possible swaps is printed (the end of the procedure) the program immediately ends.

  • Grid dimensions will be 1×1 at minimum, and have no maximum size (as limited by Int.Max or computer memory). The grid is not always square.
  • You can assume valid input. The grid will always be perfectly rectangular, containing only uppercase alphanumeric characters and #.
  • You may use a key besides Enter if it is more convenient.
  • Your program must follow [something equivalent to] the stabilization procedure, including using [pseudo]randomness for steps 1 and 2.
  • In the input (but not the output) underscores (_) may be used instead of newlines.


This is code-golf, so the shortest code in bytes wins. There are bonuses however.

Bonus 1 - Horizontal periodic boundary conditions: (minus 30 bytes)

Instead of treating the left and right out of bounds areas as #, connect them with PBC. There should be an option in your code where changing something like 0 to 1 or False to True enables or disables the PBC.

Bonus 2 - Graphics instead of text: (minus 100 bytes)

Instead of using blocks of text, use images (of any common truecolor format). Represent 0...Z with 36 distinct but related colors, e.g. a gradient from white to dark blue, and # with some other distinct color like dark red.

The program behaves the same way except now an image is either saved to a file (it may be the same file each time) or displayed to the screen for each grid swap in place of any textual output. The input may still be textual or some valid image, your choice.

Whether you do this bonus or not, giving output examples is highly encouraged.

  • \$\begingroup\$ It will be difficult to distinguish end of input. We can't read to eof because we need to wait for enter on stdin. Can we use a row count, or advance the simulation on a time step? \$\endgroup\$ – Keith Randall Oct 19 '14 at 0:58
  • \$\begingroup\$ @KeithRandall You'll know that the input is over when you receive a blank line on stdin, which signals that you should output the iteration. \$\endgroup\$ – Justin Oct 19 '14 at 5:57
  • \$\begingroup\$ Can we print all iterations at once instead of relying on Enter key ? \$\endgroup\$ – Optimizer Oct 19 '14 at 18:41
  • \$\begingroup\$ @Optimizer Nope \$\endgroup\$ – Calvin's Hobbies Oct 19 '14 at 19:51

Python, 300 298 chars

import random
while S:
 for i,c in enumerate(G):
    B=[(d,j)for j,d in enumerate(G[i+w:i+w+3])if'/'<d<c]
    if B:S+=[(i,[i+w+j for d,j in B if d==min(B)[0]])]
 if S:i,B=C(S);j=C(B);G[i],G[j]=G[j],G[i]

Run it like cat inputfile - | fluid.py. The input file should be a single line using _ as a line separator.

  • \$\begingroup\$ You can save two bytes by doing S=R=raw_input instead of S=1 \$\endgroup\$ – Claudiu Oct 19 '14 at 16:27
  • \$\begingroup\$ pastebin.com/6BQJGAxc With these modifications, it is down to 285 chars. The input file is still delimited by _ but must now also end with a trailing _. A full input file could thus look like A01_#10_020_\n \$\endgroup\$ – Christian Sonne Oct 20 '14 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.