Given a setup of dominoes your task is to figure out which dominoes fall and which don't.
Input
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):
Q W E
A D
Z X C
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:
D||||/
- /
- -
- -
/|||||
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.
Output
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.
Rules
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.
Examples:
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.
Examples:
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.
Examples:
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)
Examples
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).