Given a setup of dominoes your task is to figure out which dominoes fall and which don't.


Take a rectangular ASCII representation of a domino setup. The following characters are used for the ASCII grid:

  • (space): empty cell
  • |, -, /, \: dominoes

Dominoes can fall in 8 directions, which are represented by the following letters (similar to WASD orientations):

A   D

One or more of the dominoes will be replaced by one of these letters to indicate that the domino is pushed over at the start. Here is an example:

  -   / 
  -    -
  -    -

I don't want this challenge to turn into an exercise in input parsing so any of the following input forms are allowed:

  • A string with the grid (optionally preceded by its dimensions if that helps)
  • An array/list/tuple with one string for each line (optionally along with width and height integers)
  • A (nested) array/list/tuple with one string/character for each grid cell (optionally along with width and height variables)

You can read from STDIN or take a function argument or even expect the input to be stored in a variable.


Write to STDOUT or return (or save in a variable) the resulting grid in any of the valid input formats, indicating which dominoes have fallen and which haven't. That is, replace each fallen domino with # and leave each standing domino as it was in the input.


Of course, dominoes propagate their fall through the setup. As there may be race conditions, we assume that there are fixed time steps and the falling propagates one grid cell per time step.

Dominoes generally fall the way you would intuitively expect them to, but a rigorous specification of common sense turns out to be quite lengthy. Sorry for that, I hope the examples help. Here is a gist with all unique two-tile combinations (up to rotation and reflection). Read on for the rigorous rules.

Each domino can fall in only two directions:

           W       Q          E
A | D      -        /        \
           X         C      Z

Whenever a domino falls it affects the cell in the direction of the fall. If that cell contains a domino which can fall in the same direction or in a direction which differs by 45 degrees, that domino does so in the next time step.


D|    ->    DD      (falls in same direction)

D/    ->    DC      (falls at 45 degrees)

C     ->    C       (falls at 45 degrees)
 -           X

Whenever a diagonally oriented domino (/ or \) falls, it also affects the two cells which touch both its cell and the cell in the direction of its fall. If these cells contain a domino which can fall in the same direction as the original domino, or in the axis-aligned direction away from it, that domino does so in the next time step.


C/     ->   CC      (the cell in the direction of the fall is the one below
                     the /, so it falls in the same direction)

C|     ->   CD      (falls in the axis-aligned direction away from the C)

C-     ->   C-       (the direction away from the Q is W, 
  or                  but neither - nor \ can fall to W)
C\     ->   C\     

Exception: if a domino is pushed in both valid directions at once (i.e. if any of the above rules are in conflict), it doesn't fall.


D|A   ->    D|A     (central domino in pushed in both directions)

  Z           Z     (although it's pushed in one direction by two dominoes
D\A   ->    D\A      and in the other by only one, it doesn't fall)

 X           X      (the domino is being pushed from two opposing sides
D-A   ->    DXA      D and A, but neither is valid for that domino. Hence,
                     there is no conflict and the X can push the domino over)

 Z           Z      (pushed in the same direction by two dominoes, so falls)
\A    ->    ZA

   Z           Z           Z    (once the conflict arises, the affected
  \   ->      Z   ->      Z      domino is locked in its position and can't
D|A         D|A         D|A      be pushed over by future dominoes)


8 5
D||||/            ######  
  -   /             -   # 
  -    -    -->     -    #
  -    -            -    #
  /|||||            /|||||


17 9
E|/|||/                    #######          
  -   -                      #   #          
  -   -                      #   #          
  -   -                      #   #          
  /|||/|||||||||/    -->     ###############
       /        -                 #        #
        /       -                  #       -
         /      -                   #      #
          /|||||\                    #######


19 8
       \|/                        ###           
       - -                        # #           
D||||/|\ /|||/             ######## #####       
      /      -                   #      #       
       -    \-       -->          -    \#       
      \-   \ -                   #-   \ #       
D||||\ /  \  /             ###### /  \  #       
        |\    |||||                |\    #####  


11 11
-\\\/|\|\-|         -\##/|###-|
-|\\||\-|\-         -|#####-|\-
|\//\//|-/-         |#//\//|#/-
\|//|-|\-\|         #####-|##\|
---||/-\//|         #-######//|
///|||\----   -->   #/#####----
-|/---|-|-\         #|##--|-|-\
--|--\/|///         ####-\/|///
/|//--|//-|         ####--|//-|
|/\-|||-/-\         |/\####-/-\
E||\-|\---/         ####-|\---/

Let me know if you think I made a mistake (especially with the last one).

up vote 14 down vote accepted

C# 1048 907 850bytes

Now heavily golfed, pretty much just a jumble of bit operations on a two-dimensional array of integer enums. Could probably be made somewhat shorter by using a 1-dimensional array, but I'm not prepared to try and re-work this at this point. Reads the dimensions and string from stdin, for example:

11 11 -\\\/|\|\-|-|\\||\-|\-|\//\//|-/-\|//|-|\-\|---||/-\//|///|||\-----|/---|-|-\--|--\/|////|//--|//-||/\-|||-/-\E||\-|\---/


using L=System.Console;class R{static void Main(){int p=255,e,c,E=7,D,C,X,Z,A,m=-1,t=m,f=t,u,i=t;for(;t<0;)for(t=f,f=0;(C=L.Read())>47;)f=f*10+C-48;var T=new int[f,t];for(;++i<f;)for(c=0;c<t;T[i,c++]=(C>99?68:C>91?34:C>89?112:C>87?56:C>86?131:C>80?193:C>68?7:C>67?14:C>66?28:C>64?224:C>46?136:C>44?17:0)*(C>64&C<91?1:257))C=L.Read();for(;i+E>0;E=-E)for(i=c=m;++c<f;)for(C=m;++C<t;){if(E>0&(A=D=T[c,C])>0&D<p){T[c,C]=m; X=C+(i=(D&4)>0?1:(D&64)/-64);Z=c+(u=(D&16)>0?1:D%2*m);System.Action v=()=>{if(Z>m&Z<f&X>m&X<t&&(e=T[Z,X])>p&(e>>8&A)>0)T[Z,X]&=65280|A;};v();if((i&u)!=0){X=i==u?C:X;Z=i==u?Z:c;A=((D&128)/128+D*2)&D;v();X=C+i-X+C;Z=c+u-Z+c;A=(D%2*128+D/2)&D;v();}i=8;}if(E<0&D>p&((D=D&p)&(D-1))<1&D>0)T[c,C]=D<2?131:D>64?193:D/2*7;}for(D=m;++D<f;L.WriteLine())for(c=0;c<t;L.Write(C<0?'#':(C=C>>8)>99?'/':C>67?'|':C>33?'\\':C>9?'-':' '))C=T[D,c++];}}

Because I have too much time I've modified the ungolfed version to output an animated gif of the dominoes falling (with help from this SO question and these documents 1 2). This has only added code, and it's either in a #if gifness block or clearly marked.

To create the gifs you provide a pair of command line arguments describing the frame times, output file, etc,

dominoGolf.exe console_delay (out_file_name (gif_frame_time (final_frame_time)))
dominoGolf.exe 0 outfile.gif 1 100

The third argument is the frame time for each frame (1/100s of seconds). The fourth argument is the frame time for last frame (1/100s of seconds). The second argument is the output filename for the gif. You can omit the filename, delay, and final delay if you just want the console output. The first argument is a delay between frames rendered to the terminal in milliseconds. You can omit all arguments if you don't want an animation at all and just want to see the result. It reads the domino data from stdin just like the golfed version. This code is horrible in it's own way (butchers the gif so that it loops).

Ungolfed gif-generating code:

#define gifness

using L=System.Console;

class R
    static void Main(string[] args) // don't need args
        int p=255,P=0xFF00,m=15,M=240, // might be able to inline a couple of these
        w=1,e=2,d=4,c=8,x=16,z=32,a=64,q=128, // most of these are reusable
        W=131,E=7,D=14,C=28,X=56,Z=X*2,A=Z*2,Q=193, // most (all?) of these are reusable
        Y=w+x,U=a+d,N=c+q,B=z+e, // one of these atleast is pre-evalable

        // recognise this?

        var T=new int[f,t]; // main arr
        // domino: dir, copy(for render)
        // motion: pickup

        // input
        for(;++i<f;) // values of i and j don't matter, just counters
            for(j=0;j<t;) // increment done 3down
                // fallen
                // dominos
                k==' '?0:k=='-'?Y:k=='/'?N:k=='|'?U:B // ASCII, order for >

        #if gifness
        System.Drawing.Font font1 = null;
        System.Windows.Media.Imaging.GifBitmapEncoder genc = null;
        System.Drawing.Bitmap bmp = null;
        System.Drawing.Graphics g = null;
        if (args.Length > 1)
            font1 = new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            genc = new System.Windows.Media.Imaging.GifBitmapEncoder();
            bmp = new System.Drawing.Bitmap(t * 8, f * 14); // I have no clue what these should be in relation to em size
            g = System.Drawing.Graphics.FromImage(bmp);

        if (args.Length > 0) // not important

        // main
        for(;i>0;) // can do i=1-i and swap setting 1 for 0 to remove {}

            if (args.Length > 0) // non-critical, renders the current state to the console/gif
                var os="";
                for (o=0;o<f;o++) // values of i and j don't matter, just counters
                    for (j=0;j<t;j++)
                        os += k==0?' ':k<p?'#':(k=k>>8)==Y?'-':k==N?'/':k==U?'|':'\\'; // order for >
                L.SetCursorPosition(0, 0);

                #if gifness
                if (args.Length > 1)
                    g.DrawString(os, font1, System.Drawing.Brushes.Black, 0, 0);
                    System.IO.MemoryStream bms = new System.IO.MemoryStream();
                    bmp.Save(bms, System.Drawing.Imaging.ImageFormat.Gif);
                    var bmpf = System.Windows.Media.Imaging.BitmapFrame.Create(bms);

                    // do I chose the frame duraton1??!?!?! (take from args[2] is present else args[0])


            // back to important stuff
            i=0; // set me to 1 if we do anything (8 in golfed version due to E being 7)

            // move motions
            for (j=0;j<f;j++) // careful!!
                for (k=0;k<t;k++) // careful!!
                    if (o>0&o<p) // we are motion
                        T[j,k]=-1; // do this so we can't skip it


                        System.Action v=()=>{

                        if (i!=0&u!=0)
                            K=i==u?k:K; // k+i == K
                            J=i==u?J:j; // j+u == J



            // move dominos
            for (j=0;j<f;j++) // careful!!
                for (k=0;k<t;k++) // careful!!
                    if (o>p) // we are domino
                        if ((o&m)<1!=(o&M)<1)
                        { // we have motion

        if (args.Length > 0)
            L.SetCursorPosition(0, 0);

        // output
        for (o=0;o<f;o++)
            for (j=0;j<t;j++)
                L.Write(k<0?'#':(k=k>>8)==0?' ':k==Y?'-':k==N?'/':k==U?'|':'\\'); // order for >

        #if gifness
        if (args.Length > 1)

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            byte[] data = ms.GetBuffer();
            byte[] netscape = { 0x21, 0xFF, 0x0B, 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, 0x03, 0x01, 0x00, 0x00, 0x00 };

            if (args.Length > 2)
                int last = -1;
                int duration = int.Parse(args[2]);

                // promise yourself now you will never use this in production code
                // I've not read enough of the GIF spec to know if this is a bad idea or not
                for (i = 0; i < ms.Length - 5; i++)
                    if (data[i] == 0x21 && data[i+1] == 0xF9 && data[i+2] == 0x04 && data[i+3] == 01)
                        data[i+4] = (byte)(duration & p); // something endian (least significant first)
                        data[i+5] = (byte)((duration & P) >> 8);
                        last = i+4;

                if (last != -1 && args.Length > 3)
                    duration = int.Parse(args[3]);
                    data[last] = (byte)(duration & p);
                    data[last+1] = (byte)((duration & P) >> 8);

            using (System.IO.FileStream fs = new System.IO.FileStream(args[1], System.IO.FileMode.Create))
                fs.Write(data, 0, 13);

                // behold
                fs.Write(netscape, 0, netscape.Length);

                fs.Write(data, 13, (int)ms.Length - 13); // lets hope these arn't in excess of 2GBs

An example gif for the 3rd example input (and that shown above)

3rd example

Example randomly generated 100x25 domino layout

100x25 domino layout

"Domino" in dominoes

"Domino" in dominoes

Python 1188

Basically just loops through continuously matching some hefty regex until it stops changing. It actually matches all the pushes of each direction separately (through regex), and then composes the different results making sure there are no conflicts and such.

The regex can probably be done much more compactly, but for now here is what I have (it assumes the grid is stored in a string, g, and the dimensions are in x and y):

import re;L='QWEADZXC';a='';m=(x*y+y);o=[0]*m;X='(.{%s})';t=X%x;u=X%(x+1);v=X%(x-1);R=range
def S(s,l):
 for p,r in l:
    for i in R(len(n)):
     if n[i]!=s[i]:f=f[:i]+n[i]+f[i+1:]
 return f
while g != a:
 for i in range(m):
    for r in d:
     if r[i]in L:
        if c==0:c=r[i]
        elif r[i]!=c:o[i]=1;break
    n+=g[i]if c==0 or o[i]else c
print re.sub(r'\w','#',g)

More ungolfed:

import re


def sub_all(string,lst):
    final = string
    for p,r in lst:
        new = re.sub(p,r,string,flags=re.DOTALL)
        for i in range(len(new)):
            if new[i]!=string[i]:
    return final

def dominoes(grid,x,y):
    print len(grid),x*y+y
    print grid

    last = ''
    locked = [0]*(x*y+y)
    while grid != last:
        last = grid

        Dgrid = sub_all(grid,[(r'D\|','DD'),(r'D\\','DE'),('D/','DC')])
        Agrid = sub_all(grid,[(r'\|A','AA'),(r'\\A','ZA'),('/A','QA')])
        Wgrid = sub_all(grid,[('-(.{%s})W'%x,r'W\1W'),(r'\\(.{%s})W'%x,r'E\1W'),('/(.{%s})W'%x,r'Q\1W')])
        Xgrid = sub_all(grid,[('X(.{%s})-'%x,r'X\1X'),(r'X(.{%s})\\'%x,r'X\1Z'),(r'X(.{%s})/'%x,r'X\1C')])

        Cgrid = sub_all(grid,[('C(.{%s})/'%(x+1),r'C\1C'),('C(.{%s})-'%(x+1),r'C\1X'),(r'C(.{%s})\|'%(x+1),r'C\1D'),

        Zgrid = sub_all(grid,[(r'Z(.{%s})\\'%(x-1),r'Z\1Z'),('Z(.{%s})-'%(x-1),r'Z\1X'),(r'Z(.{%s})\|'%(x-1),r'Z\1A'),

        Qgrid = sub_all(grid,[('/(.{%s})Q'%(x+1),r'Q\1Q'),('-(.{%s})Q'%(x+1),r'W\1Q'),(r'\|(.{%s})Q'%(x+1),r'A\1Q'),

        Egrid = sub_all(grid,[(r'\\(.{%s})E'%(x-1),r'E\1E'),('-(.{%s})E'%(x-1),r'W\1E'),(r'\|(.{%s})E'%(x-1),r'D\1E'),

        grids = [Dgrid,Agrid,Wgrid,Xgrid,Cgrid,Zgrid,Qgrid,Egrid]
        ngrid = ''

        for i in range(x*y+y):
            c = None
            for g in grids:
                if g[i] in L:
                    if c==None: c = g[i]
                    elif g[i] != c:
                        ngrid += grid[i]
                ngrid += grid[i] if c==None or locked[i] else c
        grid = ngrid
        print grid
    return re.sub(r'\w','#',grid)

The listed inputs all produce their outputs except the third, in which I'm pretty sure there's a mistake (the \-/ at the very top should be \|/ if the given output is desired). I'm also assuming that the . in the bottom left corner of the last one was intended to be a D.

  • You're right, those were two typos, although the last one was supposed to be an E (not that it makes a difference...). It looks like you can save a bunch of characters by reducing the indentation depths to the bare minimum. – Martin Ender Jul 23 '14 at 6:44
  • I'm not sure whether I made some mistake copy-pasting the third example, but the backslashes won't fall their (they seemed to work in the fourth example though) – Martin Ender Jul 23 '14 at 6:54
  • 1
    @MartinBüttner, though I did have some trouble copy-pasting (make sure there is no trailing whitespace at the end of lines, and no tabs, only spaces) it seems to work once I got it correct. It might help to have the example inputs in a format easier to copy-paste, without the expected results on the same lines. – KSab Jul 23 '14 at 20:08

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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