J - 32 26 char
J can't handle more than two arguments without using a list, and it can't handle non-homogenous lists without boxing. So having the input as a list of three integers is ideal. The parameter order is the reverse of the standard one: 0 for left or 1 for right, then position, then total number of dominoes.
The reason for this is because J will end up going through them right-to-left.
{`(('|/\'{~-@>:,:<:)1+i.)/
I will add a more in-depth explanation later, but here's the gist of what's happening: F`G/
applied to a list x,y,z
will evaluate x F (y G z)
. y G z
constructs both possible ways the dominoes could have toppled, and then F
uses x
to select which of the two to use.
{`(('|/\'{~-@>:,:<:)1+i.)/ 1 3 10
||////////
{`(('|/\'{~-@>:,:<:)1+i.)/ 0 3 10
\\\|||||||
At the expense of a few characters, we can make the order the standard order: just append @|.
to the end of the function. Adapting this to work with a string argument for direction would be much more costly, however.
if(third_parameter)
instead ofif(third_paramter=='l')
– user80551 20 hours ago