# Write a domino effect

Using the fewest Unicode characters, write a function that accepts three parameters:

• Total number of dominoes
• nth affected domino
• Topple direction of the affected domino (0 or L for left, 1 or R for right)

Once a domino is toppled, it must also topple the remaining dominoes in the same direction.

You should output the dominoes with | representing a standing domino and \ and / representing a domino toppled to the left and right respectively.

## Examples

10, 5, 1 should return ||||//////
6, 3, 0 should return \\\|||

• Should the third parameter be a string or will a bool/int do like 0:left , 1:right? – user80551 Jul 9 '14 at 18:11
• Your example suggests that if there are 10 dominoes, and 5 are knocked right, we should display six of the ten dominoes knocked over. – algorithmshark Jul 9 '14 at 18:21
• @algorithmshark I think we should show the result if the fifth domino is knocked right. – user80551 Jul 9 '14 at 18:22
• @rybo111 Can you allow the third parameter to be an int as that can make comparison operations shorter. Simply if(third_parameter) instead of if(third_paramter=='l') – user80551 Jul 9 '14 at 18:42
• Can we choose the order of the parameters? – Justin Jul 9 '14 at 18:52

## JavaScript (ES5) - 86 chars

function f(a,p,d){r='';while(a-->p+1)r+='|';while(a-->=0)r=d>'q'?r+'/':'\\'+r;return r}


# Autoit 245 Bytes

Local $1 = "",$dir = "l",$amout = 100,$pos = 33
for $i=1 to$pos
if $dir = "r" then$1 = $1 & "|" Else$1 = $1 & "\" EndIf next for$i=1 to $amout-$pos
if $dir = "r" then$1 = $1 & "/" Else$1 = $1 & "|" EndIf next ConsoleWrite($1 & @cr)


# C# 92('\':left, '/':right)

String d(int c, int p, char d){ String r="";for(int i=0;i<c;i++)r+=(i>=c-p)?d:'|';return r;}

• Would be even shorter if you rename result and get rid of the additional whitespaces. – Padarom Jul 10 '14 at 6:01

## VBScript 10195 74

Edit: Looks like a lot of answers accept bool directions. For char directions, replace -d with +(d="r") for a score increase of 6.

function f(a,p,d):for x=1to a:f=f+mid("/|\",3-d+(x>p),1):next:end function

• This doesn't work. – JesterBLUE Jul 10 '14 at 19:28
• @JesterBLUE Thanks for pointing that out. I re-coded and shortened it a bit, too. – comfortablydrei Jul 10 '14 at 22:19
• That looks a lot nicer but it still isn't producing the right results. Try Mid("\|/",1-d-(x>p+d),1). (Also note that d is never explicitly converted to a boolean, so running f(10,4,1) will give funky results because the function relies on an implicit boolean to integer conversion. Putting in a d=d<>0 would lock d down to 'true' or 'false'. It's not a problem here, though, because this is golf.) – JesterBLUE Jul 11 '14 at 13:02
• @JesterBLUE I had to flip the symbol order to get this to work correctly. I didn't realize I broke the code when I allowed for "bool" directions. – comfortablydrei Jul 11 '14 at 16:50

# C - 62 64 ('l':left, 'r':right)

f(a,b,c){for(b=a-b;a--;)putchar(c&2?a<b?47:124:a>b-2?92:124);}


# C - 58 (0:left,1:right)

f(a,b,c){for(b=a-b;a--;)putchar(124-(a<b-!c?c*77:!c*32));}


recursive approach, but still, same length

f(a,b,c){a&&f(a-1,b,c)+putchar(124-(a<=b+!c?!c*32:c*77));}


awfully long (75) but most complex one (repeat function integrated into it)

f(a,b,c){c>1?a&&f(a-1,putchar(b),2):f(b+c,c?92:124,2)+f(a-b-c,c?124:47,2);}

• This segfaults for me, probably because printf expects a const char* instead of an int (using putchar instead works). It also looks like this always knocks the dominoes over towards the right (i.e. for 10,5,'l' it prints something like ||||\\\\\\  instead of \\\\\|||||). – Ventero Jul 9 '14 at 19:26

Using guards and the characters 'r' and 'l' as directions:

r=replicate;k a o d|d=='r'=r(a-o)'|'++r o '/'|d=='l'=r o '\\'++r(a-o)'|'


# Perl : 66 characters

My first code golf submission! Thanks for the fun challenge.

sub k{($n,$p,$r)=@_;$n-=$p;$d=$r?"|"x--$p.'/'x++$n:'\\'x$p.'|'x$n}  Explanation with whitespace: sub k{ ($n,$p,$r) = @_;      # get function arguments: <# dominos> <position> <0=left, 1=right>
$n -=$p;             # total number of dominos - position
$d =$r ? "|"x--$p . '/'x++$n : '\\'x$p . '|'x$n
# if direction = 1, make pos - 1 '|'s and (total - position  + 1) '/'s
# if direction = 0, make pos '\'s and (total - position) '|'s
# return is the last action of the function, it returns the string in $d. # Semicolon at end of line can be left off, as it is the last statement of the function }  Example usage and output: my$result = k(10,5,1); print "$result\n"; ||||////// my$result = k(10,5,0); print "$result\n"; \\\\\|||||  ## AWK, 76 61 {ORS="";for(i=1;i<$2;i++)print"|";for(i=$2;i<=$1;i++)print($3=="r"?"/":"\\")}  Usage: echo 10 5 r | awk '{ORS="";for(i=1;i<$2;i++)print"|";for(i=$2;i<=$1;i++)print($3=="r"?"/":"\\")}'  Although I'm wondering if I can nest ternary operators to make this shorter (although I'm playing around in bash right now and it doesn't seem like you can). edit -- shorter version: {ORS="";for(i=0;i<$1;i++)print i<$2?"|":($3=="r"?"/":"\\");}

• What about {ORS="";for(i=0;i<$1;i++)print i<$2?"|":($3=="r"?"/":"\\");} – Jerry Jeremiah Aug 4 '14 at 0:56 • That's exactly what I didn't think was possible. Thanks. – shadowtalker Aug 4 '14 at 1:06 ### PowerShell (107 95) filter d($a,$b,$c){-join(1..$a|%{if($_-ge$b-and$c){'/'}elseif($_-le$b-and!$c){'\'}else{'|'}})}  If we're allowed to treat the third parameter as an integer (0=l,1=r), then I can shave a little off here. Test input: d 10 5 r ||||////// d 10 5 l \\\\\|||||  Update: d 10 5 1 ||||////// d 10 5 0 \\\\\|||||  If anyone by chance wants an explanation I will gladly add one. And as always I'm open to suggestions. Original: function d($a,$b,$c){-join(1..$a|%{if($_-ge$b-and$c-eq'r'){'/'}elseif($_-le$b-and$c-eq'l'){'\'}else{'|'}})}  # Python, 69 My first code golf submission, with Python in 69 characters: def d(a,b,c):print "|"*(b-1)+"/"*((a+1)-b) if c else "\\"*b+"|"*(a-b)  Usage: d(10,5,1) • I know it's been 4 years since this was posted but you have a lot of extra whitespace involved in this answer. White space is generally not required on the borders of quotes or parentheses. Here is your code without the extra whitespace def d(a,b,c):print"|"*(b-1)+"/"*(a+1-b)if c else"\\"*b+"|"*(a-b). You could also use a lambda to make this shorter lambda a,b,c:"|"*(b-1)+"/"*(a+1-b)if c else"\\"*b+"|"*(a-b). – Post Rock Garf Hunter Jan 3 '18 at 21:11 • Ooooookkkkkkkkk – armani Jan 4 '18 at 0:28 # Python 71 59 def k(n,p,d):c,r='\/'[d],n-p;return[c*p+'|'*r,'|'*p+c*r][d]  # PHP, 55 bytes for([,$c,$n,$d]=$argv;$c--;)echo"\|/"[$d+(++$i+$d>$n)];

• requires PHP 7.1 or later
• replace [...]=$argv with list(...)=$argv for PHP 5.5 or later
• for older PHP, insert $s="\|/", before that and replace the other "\|/" with $s.

Run with php -nr '<code>' <total> <N> <numeric direction> or try it online.