Write a domino effect

25

2

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

rybo111

Posted 2014-07-09T17:56:13.757

Reputation: 4 071

Should the third parameter be a string or will a bool/int do like 0:left , 1:right? – user80551 – 2014-07-09T18:11:11.333

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 – 2014-07-09T18:21:57.807

1@algorithmshark I think we should show the result if the fifth domino is knocked right. – user80551 – 2014-07-09T18:22:41.383

@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 – 2014-07-09T18:42:46.797

Can we choose the order of the parameters? – Justin – 2014-07-09T18:52:18.450

Alright, use whichever parameter order and the direction can be an int or a string – rybo111 – 2014-07-09T18:58:59.213

And my example is correct: if you knock over the 5th domino, to the right, it also knocks over the remaining dominos to the right. – rybo111 – 2014-07-09T18:59:36.763

Shouldn't right make //////|||| ? Your example doesnt start at the beginning... – Martijn – 2014-07-10T14:06:31.913

@Martijn The example parameters are 10, 5, 'r' - this translates to: There are 10 dominos. Knock the 5th one to the right. This means the 5th domino becomes knocked over (/) as does everything after it. However, if you change the r to an l, the output would be \\\\\||||| because when you knock a domino left it would knock over everything before it instead. – rybo111 – 2014-07-10T14:35:12.240

IMO a bit weird principle for dominos. I Interpreted it as 10 dominos, 5 have allready fallen in the direction R. Guess I'll have to update my code – Martijn – 2014-07-10T14:46:37.263

@Martijn Well, the principle of the domino effect is that one change causes others to change. I'd rather specify one change than specify all the changes – rybo111 – 2014-07-10T15:01:04.977

Mine was assuming the items where still falling, more like s snapshot. But updated my code – Martijn – 2014-07-10T15:05:05.073

@Martijn if it's any consolation, I interpreted as you did as first as well ^.^ – Cruncher – 2014-07-10T18:53:06.857

Answers

14

Ruby, 38 (46) characters

e=->n,k,r{k-=r;'\|'[r]*k+'|/'[r]*n-=k}

This function takes the direction as an integer (1 for right, 0 for left). A function that takes a string is 8 characters longer:

d=->n,k,r{n-=k;r<?r??\\*k+?|*n :?|*~-k+?/*-~n}

Usage examples:

puts e[10, 5, 1] # or d[10, 5, 'r']
||||//////
puts e[10, 5, 0] # or d[10, 5, 'l']
\\\\\|||||

Ventero

Posted 2014-07-09T17:56:13.757

Reputation: 9 842

why are there only 5 dominoes knocked left in the second example? – Clyde Lobo – 2014-07-10T16:06:31.493

1@ClydeLobo Because you start at position 5 and knock the domino to the left, which in turn knocks over the 4 dominoes to its left, for a total of 5. In the first example, starting at position 5 knocks over 6 dominoes: The one at position 5 plus the 5 to its right. – Ventero – 2014-07-10T16:10:00.597

8

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.)/

Here's what's going on. 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.

Below is a back-and-forth with the J REPL that explains how the function is built together: indented lines are input to the REPL, and responses are flush with the left margin. Recall that J evaluates strictly right to left unless there are parens:

   1 ] 3 (]) 10            NB. ] ignores the left argument and returns the right
10
   1 ] 3 (] 1+i.) 10       NB. hook: x (F G) y  is  x F (G y)
1 2 3 4 5 6 7 8 9 10
   1 ] 3 (>: 1+i.) 10      NB. "greater than or equal to" bitmask
1 1 1 0 0 0 0 0 0 0
   1 ] 3 (-@>: 1+i.) 10    NB. negate
_1 _1 _1 0 0 0 0 0 0 0
   1 ] 3 (<: 1+i.) 10      NB. "less than or equal to"
0 0 1 1 1 1 1 1 1 1
   1 ] 3 ((-@>:,:<:)1+i.) 10          NB. laminate together
_1 _1 _1 0 0 0 0 0 0 0
 0  0  1 1 1 1 1 1 1 1
   1 ] 3 (('|/\'{~-@>:,:<:)1+i.) 10   NB. turn into characters
\\\|||||||
||////////
   1 { 3 (('|/\'{~-@>:,:<:)1+i.) 10   NB. select left or right version
||////////
   {`(('|/\'{~-@>:,:<:)1+i.)/ 1 3 10  NB. refactor
||////////
   {`(('|/\'{~-@>:,:<:)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:

   |. 10 3 1
1 3 10
   {`(('|/\'{~-@>:,:<:)1+i.)/@|. 10 3 1
||////////

Adapting this to work with a string argument for direction would be much more costly, however.

algorithmshark

Posted 2014-07-09T17:56:13.757

Reputation: 8 144

I know it's been a while since you wrote this answer, but the way it is structured is very cool. I really like how you made use of gerunds and / and also the way you build two outputs and select the desired one. I guess I feel like this lacks the recognition it deserves. – cole – 2018-01-04T03:04:00.843

What @cole said, I was awed. – FrownyFrog – 2018-01-04T07:13:22.813

8

Haskell, 70

f R i l=(i-1)#'|'++(l-i+1)#'/'
f L i l=i#'\\'++(l-i)#'|'
(#)=replicate

assuming there is a type Direction, which has constructors R and L.

proud haskeller

Posted 2014-07-09T17:56:13.757

Reputation: 5 866

7

PowerShell, 66

filter d($n,$k,$d){"$('\|'[$d])"*($k-$d)+"$('|/'[$d])"*($n-$k+$d)}

Probably the same idea every one else had.

  • Takes either 0 or 1 as the direction parameter (for left and right, respectively)

Joey

Posted 2014-07-09T17:56:13.757

Reputation: 12 260

6

Golfscript (44 53)

My first ever Golfscript program. Took me way longer than it should have and can probably be done in a smarter, more concise way (I'm sure someone will prove that :) ):

:d;:j;:^,{:x j<d&'\\'{x^j)->d!&'/''|'if}if}%

A sample input is 10 5 0.

Ungolfed:

:d;:j;:^      # save input in variables and discard from stack, except total length ^
,             # create an array of numbers of length ^
{             # start block for map call
  :x          # save current element (= index) in variable
  j<          # check whether we are left of the first knocked over domino
  d           # check whether the direction is to the left
  &           # AND both results
  '\\'        # if true, push a backslash (escaped)
  {           # if false, start a new block
    x^j)->    # check whether we are on the right of the knocked over domino
    d!        # check whether the direction is to the right
    &         # AND both results
    '/'       # if true, push a slash
    '|'       # if false, push a non-knocked over domino
    if
  }
  if
}%            # close block and call map

Ingo Bürk

Posted 2014-07-09T17:56:13.757

Reputation: 2 674

1Proof done ;-) although I am not happy with my solution yet. – Howard – 2014-07-10T06:06:42.047

1Some tips: you may choose d to be 0/1 instead of 'l'/'r' which gives you some shorter code. Otherwise, if you store d'l'= in a variable oyu may use it instead of the second comparison with d. In the term x i j you can save both whitespaces if you use a non-alphanumeric variable name instead of i. – Howard – 2014-07-10T06:12:00.500

@Howard Thanks for the tips! I chose 'l'/'r' because at the time I didn't see yet that we are free to use integers. The non-alphanumeric trick is slick, thanks! Maybe I'll update the answer later. – Ingo Bürk – 2014-07-10T06:14:29.090

4

JS (ES6) - 79 74 72 65 62

thanks to @nderscore!

The 3rd param is a boolean (0: left / 1: right)

d=(a,b,c)=>"\\|"[a-=--b,c].repeat(c?b:a)+"|/"[c].repeat(c?a:b)

// Test
d(10,3,1); // => "||////////"
d(10,3,0); // => "\\\\\\\\||"

xem

Posted 2014-07-09T17:56:13.757

Reputation: 5 523

1this entry could be a reference card for ECMAScript 6 :D – bebe – 2014-07-09T19:00:14.180

@bebe haha, and it's not even its final form. ES6 can be very dirty. – xem – 2014-07-09T19:08:38.800

165: d=(a,b,c)=>"\\"[r="repeat"](!c&&a-b+1)+"|"[r](--b)+"/"[r](c&&a-b) – nderscore – 2014-07-10T03:47:55.760

1great! I also found this crazy thing but it's longer (67): d=(a,b,c,d=a-b+1)=>"\|"[c].repeat(c?b-1:d)+"|/"[c].repeat(c?d:b-1) – xem – 2014-07-10T07:04:06.960

I don't think repeat aliasing is worth. [r='repeat'][r] 15 chars. .repeat.repeat 14 chars – edc65 – 2014-07-10T15:33:08.490

@edc65 but .repeat is used three times in the first function. That's why I made the alias – xem – 2014-07-10T15:36:47.877

62: d=(a,b,c)=>"\\|"[a-=--b,c].repeat(c?b:a)+"|/"[c].repeat(c?a:b) – nderscore – 2014-07-10T16:03:49.953

@nderscore hey. you beat me once again. Stop that. lol :) thanks, it's awesome. – xem – 2014-07-10T16:04:47.447

4

Python - 45 52

This requires 1 for right and 0 for left.

x=lambda n,k,d:'\\|'[d]*(k-d)+"|/"[d]*(n-k+d)

Here's a version that takes r and l correctly, at 58:

def x(n,k,d):d=d=='r';return'\\|'[d]*(k-d)+"|/"[d]*(n-k+d)

Some usage examples...

>>> print(x(10,3,0))
\\\|||||||
>>> print(x(10,3,1))
||////////
>>> print(x(10,5,1))
||||//////
>>> print(x(10,5,0))
\\\\\|||||
>>> print(x(10,3,0))
\\\|||||||

cjfaure

Posted 2014-07-09T17:56:13.757

Reputation: 4 213

4

GolfScript, 28 23 characters

'\\'@*2$'|/'*$-1%1>+@/=

Arguments on top of stack, try online:

> 10 5 1
||||//////

> 10 5 0
\\\\\|||||

Howard

Posted 2014-07-09T17:56:13.757

Reputation: 23 109

Amazing. Love to learn from all these golfscript solutions :) – Ingo Bürk – 2014-07-10T06:15:31.353

3

Haskell, 42 bytes

(n%k)b=["\\|/"!!(b-div(k-b-c)n)|c<-[1..n]]

Try it online!

Takes input like (%) n k b for n dominos, k'th domino toppled, direction b.

Finds the character at each position c ranging from 1 to n by using an arithmetic expression to compute the character index 0, 1, or 2.

Test cases taken from here.


Haskell, 44 bytes

(n%k)b=take n$drop(n+n*b+b-k)$"\\|/"<*[1..n]

Try it online!

An interesting strategy that turned out a bit longer. Generates the string "\\|/"<*[1..n] with n consecutive copies of each symbol, then takes a slice of n contiguous characters with start position determined arithmetically.

xnor

Posted 2014-07-09T17:56:13.757

Reputation: 115 687

3

Python2/3 - 54

That last added on rule was quite nice (the 0/1 instead of 'l'/'r'). Made mine actually smaller than the existing python solution. 0 is left, 1 is right

def f(a,b,c):d,e='\|/'[c:2+c];h=b-c;return d*h+e*(a-h)

# Usage:
print(f(10,5,1)) # => ||||//////
print(f(10,5,0)) # => \\\\\|||||

pseudonym117

Posted 2014-07-09T17:56:13.757

Reputation: 1 053

2

Haskell, 57 bytes

4 bytes saved thanks to this tip

f 0 b _=[]
f a b c=last("|/":["\\|"|b>c])!!c:f(a-1)(b-1)c

Try it online!

Haskell, 69 61 60 58 bytes

(0!b)_=[]
(a!b)c|b==c=a!b$c+1|1>0="\\|/"!!c:((a-1)!(b-1))c

Try it online!

Not a very complex answer but it beats both of the existing Haskell answers.

Post Rock Garf Hunter

Posted 2014-07-09T17:56:13.757

Reputation: 55 382

2

R, 75 68 61 57 bytes

An anonymous function. I'll post a fuller explanation if there is interest.

function(t,n,d)cat(c("\\","|","/")[(1:t>n-d)+1+d],sep="")

Try it online!

rturnbull

Posted 2014-07-09T17:56:13.757

Reputation: 3 689

2

Haskell, 51 bytes

f a b c=("\\|"!!c<$[1..b-c])++("|/"!!c<$[b-c..a-1])

a = number of dominoes, b = 1-based index of the touched one, c = direction (0 is left and 1 is right).

Try it online!

Max Yekhlakov

Posted 2014-07-09T17:56:13.757

Reputation: 601

Defining an infix operator also works for more than two inputs: (a#b)c= .... – Laikoni – 2018-10-13T08:28:15.630

2

Python 2.7, 68 65 61 59 58 chars

Use d=1 for left and d=0 for right

f=lambda a,p,d:['|'*(p-1)+'/'*(a-p+1),'\\'*p+'|'*(a-p)][d]

Note: Thanks to @TheRare for further golfing it.

user80551

Posted 2014-07-09T17:56:13.757

Reputation: 2 520

1Why not d and'\\'...or'/'...? – seequ – 2014-07-09T19:21:51.183

You could also do ('\\'...,'/'...)[d] – seequ – 2014-07-10T07:59:53.297

@TheRare I would need two of those lists. – user80551 – 2014-07-10T08:55:07.930

I don't think so. f=lambda a,p,d:('|'*(p-1)+'/'*(a-p+1),'\\'*p+'|'*(a-p))[d] – seequ – 2014-07-10T09:11:10.970

@TheRare Also, I don't think your code works when falling left. Could you give a test case to prove? – user80551 – 2014-07-10T09:16:00.530

2

Perl, 67 65 Characters

sub l{($t,$p,$d)=@_;$p-=$d;($d?'|':'\\')x$p.($d?'/':'|')x($t-$p)}

Assign the first three params (total, position, direction as an integer [0 left, 1 right]). Extras go into the ether. Subtract 1 from the position if we're headed right so the domino in position X is flipped, too.

titanofold

Posted 2014-07-09T17:56:13.757

Reputation: 121

1replace $p--if$d with $p-=$d to lose two characters :) – chinese perl goth – 2014-08-04T14:35:24.010

2

JavaScript (ES6) 61 63

Edit It was buggy - shame on me.

Not so different from @xem, but found it myself and it's shorter. Parameter d is 0/1 for left/right

F=(a,p,d,u='|'.repeat(--p),v='\\/'[d].repeat(a-p))=>d?u+v:v+u

Test In Firefox console

for(i=1;i<11;i+=3) console.log('L'+i+' '+F(10,i,0) + ' R'+i+' '+ F(10,i,1))

Output

L1 \\\\\\\\\\ R1 //////////
L4 \\\\\\\||| R4 |||///////
L7 \\\\|||||| R7 ||||||////
L10 \||||||||| R10 |||||||||/

edc65

Posted 2014-07-09T17:56:13.757

Reputation: 31 086

1Should it be --p? – nderscore – 2014-07-10T16:48:07.507

@nderscore yes it should, got parameters wrong, silly me. – edc65 – 2014-07-11T09:36:09.880

2

Javascript, 46 characters

Seems like cheating to do 0=l and 1=r but there is is. Shrunk it with a little recursion.

f=(a,p,d)=>a?'\\|/'[(p-d<1)+d]+f(a-1,p-1,d):''

edit: missed an obvious character

user29119

Posted 2014-07-09T17:56:13.757

Reputation: 61

1

J, 23 21 19 bytes

'|/\'{~{:-(>i.)~`-/

Try it online!

Input is a list of integers in the standard order.

FrownyFrog

Posted 2014-07-09T17:56:13.757

Reputation: 3 112

1

05AB1E, 19 bytes

αα©„\|³è×¹®-„|/³è×J

I still have the feeling it's a bit long, but it works.. And better than the initial 23 bytes solution I had with if-else construction, which I quickly dropped..

Input order is the same as in the challenge: total length, index, 1/0 for left/right respectively.

Try it online or verify both test cases.

Explanation:

α                     # Take the absolute difference of the first two (implicit) inputs
                      #  i.e. 10 and 5 → 5
                      #  i.e. 6 and 3 → 3
 α                    # Then take the absolute difference with the third (implicit) input
                      #  i.e. 5 and 1 → 4
                      #  i.e. 3 and 0 → 3
  ©                   # Store this number in the register (without popping)
   „\|                # Push "\|"
      ³è              # Use the third input to index into this string
                      #  i.e. 1 → "|"
                      #  i.e. 0 → "\"
        ×             # Repeat the character the value amount of times
                      #  i.e. 4 and "|" → "||||"
                      #  i.e. 3 and "\" → "\\\"
         ¹®-          # Then take the first input, and subtract the value from the register
                      #  i.e. 10 and 4 → 6
                      #  i.e. 6 and 3 → 3
            „|/       # Push "|/"
               ³è     # Index the third input also in it
                      #  i.e. 1 → "/"
                      #  i.e. 0 → "|"
                 ×    # Repeat the character the length-value amount of times
                      #  i.e. 6 and "/" → "//////"
                      #  i.e. 3 and "|" → "|||"
                  J   # Join the strings together (and output implicitly)
                      #  i.e. "||||" and "//////" → "||||//////"
                      #  i.e. "///" and "|||" → "///|||"

Kevin Cruijssen

Posted 2014-07-09T17:56:13.757

Reputation: 67 575

1

PHP, 89 Characters

function o($a,$p,$d){for($i=0;$i<$a;$i++)echo$d==0?($i+1>$p)?'|':'\\':($i+1<$p?'|':'/');}

Just because I love PHP.

EDIT: The following code does the same.

function dominoes ($number, $position, $direction) {
    for ($i=0; $i<$number; $i++){
        if ($direction==0) {
            if (($i+1) > $position) {
                echo '|';
            } else {
                echo '\\';
            }
        } else {
            if (($i+1) < $position) {
                echo '|';
            } else {
                echo '/';
            }
        }
    }
}

TribalChief

Posted 2014-07-09T17:56:13.757

Reputation: 111

a couple of golfing tips: 1) Unnecessary parentheses at ($i+1>$p). 2) Rewriting your ternary expression to $d?($i+1<$p?'|':'/'):$i+1>$p?'|':'\\' saves another 3 bytes. Or just remove ==0 and invert directions. 3) With $i++<$a you can remove $i++ from the post condition and use $i instead of $i+1 (-6 bytes). 4) $i=0 is not necessary; but you´d have to suppress notices (option --n) if you remove it (-4 bytes). – Titus – 2018-10-12T15:57:21.280

@Titus thank you, feel free to edit and claim credit! – TribalChief – 2018-10-14T21:46:36.663

Got a more detailed version? – Martijn – 2014-07-10T14:59:41.463

1@Martijn , I edited my post to include one. – TribalChief – 2014-07-11T03:17:03.033

Now I can see what it does. Nothing too fancy, but +1 :) – Martijn – 2014-07-11T07:04:04.620

Thanks! @NPlay 's solution(s) look fancy though! – TribalChief – 2014-07-11T07:08:19.293

1

PHP - 64

function f($a,$b,$c){for($w='\|/';++$i<=$a;)echo$w[$c+($i>$b)];}

A simple loop, and echo-ing the character.

Generates a Notice: Undefined variable: i, here's another version silenting the error (65 characters) :

function f($a,$b,$c){for($w='\|/';@++$i<=$a;)echo$w[$c+($i>$b)];}

And a version withtout any error (69 characters) :

function f($a,$b,$c){for($w='\|/',$i=0;++$i<=$a;)echo$w[$c+($i>$b)];}

Other functions in PHP :

sprintf / printf padding

function f($a,$b,$c){printf("%'{${0*${0}=$c?'|':'\\'}}{$a}s",sprintf("%'{${0*${0}=$c?'/':'|'}}{${0*${0}=$a-$b+$c}}s",''));}

padding via str_pad / str_repeat functions

function f($a,$b,$c){$f='str_repeat';echo$f($c?'|':'\\',$b-$c).$f($c?'/':'|',$a-$b+$c);}
function f($a,$b,$c){echo str_pad(str_repeat($c?'|':'\\',$b-$c),$a,$c?'/':'|');}

using both printf and str_repeat functions

function f($a,$b,$c){printf("%'{${0*${0}=$c?'|':'\\'}}{$a}s",str_repeat($c?'/':'|',$a-$b+$c));}
function f($a,$b,$c){$w='\|/';printf("%'$w[$c]{$a}s",str_repeat($w[$c+1],$a-$b+$c));}

NPlay

Posted 2014-07-09T17:56:13.757

Reputation: 13

1

Scala 75 characters

def f(l:Int,p:Int,t:Char)=if(t=='l')"\\"*p++"|"*(l-p) else "|"*(l-p):+"/"*p

jcw

Posted 2014-07-09T17:56:13.757

Reputation: 195

1

CJam - 20

q~
:X-_"\|"X=*o-"|/"X=*

The main code is on the second line, the first line is just for getting the parameters from the standard input (otherwise you need to put the parameters in the code).

Try it at http://cjam.aditsu.net/

Examples:

12 4 1
|||/////////

8 5 0
\\\\\|||

Explanation:

:X stores the last parameter (0/1 direction) in variable X
- subtracts X from the knock-over position, obtaining the length of the first sequence of characters (let's call it L)
_ makes a copy of L
"\|"X= gets the character to use first: \ for X=0 and | for X=1
* repeats that character L times
o prints out the string, removing it from the stack
- subtracts L from the number of dominoes, obtaining the length of the second sequence of characters (let's call it R)
"|/"X= gets the character to use next: | for X=0 and / for X=1
* repeats that character R times

aditsu quit because SE is EVIL

Posted 2014-07-09T17:56:13.757

Reputation: 22 326

1

Common Lisp

This won't win in a code golf, but it highlights Common Lisp's justification format directive:

(lambda (n p d &aux (x "\\|/"))
   (format t "~v,,,v<~v,,,v<~>~>" n (aref x d) (+ d (- n p)) (aref x (1+ d))))

The arithmetic isn't bad: n is the total number of dominoes; p is the position of the first toppled domino; d is either 0 or 1, representing left and right (as allowed in the comments), and is used as an index into x; x is a string of \, |, and /. The format string uses two (nested) justification directives, each of which allows for a padding character. Thus:

(dotimes (d 2)
  (dotimes (i 10)
    ((lambda (n p d &aux (x "\\|/"))
       (format t "~v,,,v<~v,,,v<~>~>" n (aref x d) (+ d (- n p)) (aref x (1+ d))))
     10 (1+ i) d)
    (terpri)))

\|||||||||
\\||||||||
\\\|||||||
\\\\||||||
\\\\\|||||
\\\\\\||||
\\\\\\\|||
\\\\\\\\||
\\\\\\\\\|
\\\\\\\\\\
//////////
|/////////
||////////
|||///////
||||//////
|||||/////
||||||////
|||||||///
||||||||//
|||||||||/

Joshua Taylor

Posted 2014-07-09T17:56:13.757

Reputation: 660

0

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.

Titus

Posted 2014-07-09T17:56:13.757

Reputation: 13 814

0

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);}

bebe

Posted 2014-07-09T17:56:13.757

Reputation: 3 916

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 – 2014-07-09T19:26:06.847

0

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{'|'}})}

fuandon

Posted 2014-07-09T17:56:13.757

Reputation: 309

0

C++ 181

#define C(x) cin>>x;
#define P(x) cout<<x;
int n,k,i;char p;
int main(){C(n)C(k)C(p)
for(;i<n;i++){if(p=='r'&&i>=k-1)P('/')else if(p=='l'&&i<=k-1)P('\\')else P('|')}
return 0;}

bacchusbeale

Posted 2014-07-09T17:56:13.757

Reputation: 1 235

1You don't actually need to explicitly return 0 from main. – zennehoy – 2014-07-10T16:27:30.403

It doesn't compile for me because cin and cout are not in the global namespace - what compiler are you using? Also, C(n)>>k>>p would be shorted than C(n)C(k)C(p) wouldn't it? And if the define for P() could stringify the argument wouldn't that save characters for all the quotes? And when you compare p to 'l' and 'r': 0 and 1 would be shorter - specifically >0 instead of =='r' and <1 instead of =='l' (assuming you are ok using numbers instead of r/l - if not <'r' is still shorter than =='l' and >'l' is still shorter than =='r') – Jerry Jeremiah – 2014-08-04T00:48:47.093

@JerryJeremiah for cin and cout need "using namespace std". – bacchusbeale – 2014-08-06T01:37:11.943

I know but since it is only used twice it is shorter to qualify the functions. Neither way works on my compiler without the include. – Jerry Jeremiah – 2014-08-06T09:21:44.973

0

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

comfortablydrei

Posted 2014-07-09T17:56:13.757

Reputation: 701

1This doesn't work. – JesterBLUE – 2014-07-10T19:28:58.353

@JesterBLUE Thanks for pointing that out. I re-coded and shortened it a bit, too. – comfortablydrei – 2014-07-10T22:19:50.650

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 – 2014-07-11T13:02:02.103

@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 – 2014-07-11T16:50:11.940

0

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;}

Hakubex

Posted 2014-07-09T17:56:13.757

Reputation: 1

1Would be even shorter if you rename result and get rid of the additional whitespaces. – Padarom – 2014-07-10T06:01:40.877

0

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"?"/":"\\");}

shadowtalker

Posted 2014-07-09T17:56:13.757

Reputation: 461

1What about {ORS="";for(i=0;i<$1;i++)print i<$2?"|":($3=="r"?"/":"\\");} – Jerry Jeremiah – 2014-08-04T00:56:33.033

That's exactly what I didn't think was possible. Thanks. – shadowtalker – 2014-08-04T01:06:30.307

0

PHP - 105,97, 96

 function a($r,$l,$f){$a=str_repeat('|',$l-$f);$b=str_repeat($r?'/':'\\',$f);echo$r?$a.$b:$b.$a;}

Example results:

a(true,10,4);  -> \\\\||||||
a(false,10,5); -> |||||/////
a(false,10,2); -> ||||||||//

Martijn

Posted 2014-07-09T17:56:13.757

Reputation: 713

0

Javascript, 81 85 characters

function e(a,b,c){l='repeat';d='|'[l](--a-b++);return c>'q'?d+"/"[l](b):"\\"[l](b)+d}

First time trying codegolf, was fun thanks :)

Mat

Posted 2014-07-09T17:56:13.757

Reputation: 11

Might as well modify the function to be an ES6 function, as string repeat is ES6 (doesn;t work in Chrome). – Matt – 2014-07-11T02:03:48.270

0

JavaScript - 85 chars

function d(a,b,c){for(r=c?"\\":"/",p="",b=a-b;a--;)p+=c?a<b?"|":r:a>b?"|":r;return p}

1 = Left, 0 = Right

d(10,3,1)
\\\|||||||
d(10,3,0)
||////////
d(10,7,1)
\\\\\\\|||
d(10,7,0)
||||||////

Matt

Posted 2014-07-09T17:56:13.757

Reputation: 777

0

vb.net (~75c)

Dim f=Function(l,p,d)(If(d="l",StrDup(p,"\"),"")& StrDup(l-p-If(d="l",1,0),"|")).PadRight(l,"/")

Adam Speight

Posted 2014-07-09T17:56:13.757

Reputation: 1 234

0

Clojure, 81 chars

(defn g[a,p,d](apply str(map #(nth "\\|/"(+(if(>= % (- p d)) 1 0) d))(range a))))

user29119

Posted 2014-07-09T17:56:13.757

Reputation: 61

0

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}

Ben

Posted 2014-07-09T17:56:13.757

Reputation: 131

0

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)

Trojan

Posted 2014-07-09T17:56:13.757

Reputation: 1

0

Python 71 59

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

Griffin

Posted 2014-07-09T17:56:13.757

Reputation: 4 349

0

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)'|'

DrJPepper

Posted 2014-07-09T17:56:13.757

Reputation: 499

0

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

silicon

Posted 2014-07-09T17:56:13.757

Reputation: 1

0

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)

armani

Posted 2014-07-09T17:56:13.757

Reputation: 101

1I 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 – 2018-01-03T21:11:51.863

Ooooookkkkkkkkk – armani – 2018-01-04T00:28:08.447