25
\$\begingroup\$

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 \\\|||

\$\endgroup\$
  • \$\begingroup\$ Should the third parameter be a string or will a bool/int do like 0:left , 1:right? \$\endgroup\$ – user80551 Jul 9 '14 at 18:11
  • \$\begingroup\$ Your example suggests that if there are 10 dominoes, and 5 are knocked right, we should display six of the ten dominoes knocked over. \$\endgroup\$ – algorithmshark Jul 9 '14 at 18:21
  • 1
    \$\begingroup\$ @algorithmshark I think we should show the result if the fifth domino is knocked right. \$\endgroup\$ – user80551 Jul 9 '14 at 18:22
  • \$\begingroup\$ @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') \$\endgroup\$ – user80551 Jul 9 '14 at 18:42
  • \$\begingroup\$ Can we choose the order of the parameters? \$\endgroup\$ – Justin Jul 9 '14 at 18:52

42 Answers 42

1 2
0
\$\begingroup\$

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}
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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;}
\$\endgroup\$
  • 1
    \$\begingroup\$ Would be even shorter if you rename result and get rid of the additional whitespaces. \$\endgroup\$ – Padarom Jul 10 '14 at 6:01
0
\$\begingroup\$

VBScript 101 95 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
\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't work. \$\endgroup\$ – JesterBLUE Jul 10 '14 at 19:28
  • \$\begingroup\$ @JesterBLUE Thanks for pointing that out. I re-coded and shortened it a bit, too. \$\endgroup\$ – comfortablydrei Jul 10 '14 at 22:19
  • \$\begingroup\$ 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.) \$\endgroup\$ – JesterBLUE Jul 11 '14 at 13:02
  • \$\begingroup\$ @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. \$\endgroup\$ – comfortablydrei Jul 11 '14 at 16:50
0
\$\begingroup\$

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);}
\$\endgroup\$
  • \$\begingroup\$ 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 \\\\\|||||). \$\endgroup\$ – Ventero Jul 9 '14 at 19:26
0
\$\begingroup\$

Haskell (72)

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)'|'
\$\endgroup\$
0
\$\begingroup\$

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";
\\\\\|||||
\$\endgroup\$
0
\$\begingroup\$

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"?"/":"\\");}
\$\endgroup\$
  • 1
    \$\begingroup\$ What about {ORS="";for(i=0;i<$1;i++)print i<$2?"|":($3=="r"?"/":"\\");} \$\endgroup\$ – Jerry Jeremiah Aug 4 '14 at 0:56
  • \$\begingroup\$ That's exactly what I didn't think was possible. Thanks. \$\endgroup\$ – shadowtalker Aug 4 '14 at 1:06
0
\$\begingroup\$

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{'|'}})}
\$\endgroup\$
0
\$\begingroup\$

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)

\$\endgroup\$
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ – Post Rock Garf Hunter Jan 3 '18 at 21:11
  • \$\begingroup\$ Ooooookkkkkkkkk \$\endgroup\$ – armani Jan 4 '18 at 0:28
0
\$\begingroup\$

Python 71 59

def k(n,p,d):c,r='\/'[d],n-p;return[c*p+'|'*r,'|'*p+c*r][d]
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
1 2

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.