26
\$\begingroup\$

Introduction

The Dragon's Curve is a fractal curve that notably appears on section title pages of the Jurassic Park novel.

It can very simply be described as a process of folding a paper strip, as explained in the Wikipedia article about this curve.

The first few iterations of the generation of this curve look like this (credits to Wikipedia for the image):

enter image description here

The challenge

Write a program or function that, given an integer n as input, outputs the n-th iteration of the dragon curve as ASCII art using only the symbols _ and |

  • You have to output the figure using only |, _ and spaces. You may not output the curve as a plot or anything else.
  • You can take the input as a program argument, in STDIN or as a function parameter.
  • Inputs will always be an integer >= 0. Your program should work for reasonable values of inputs, 12 being the highest in the test cases offered.
  • The first iterations shall look like this

    • Iteration 0 is

      _
      
    • Iteration 1 is

      _|
      
    • Iteration 2 is

      |_ 
       _|
      
  • One trailing line at the end is ok. No trailing spaces allowed besides filling the line up to the rightmost character in the curve

  • No standard loopholes abuse as usual

Test Cases

  • Input 0

Output

_
  • Input 3

Output

   _   
|_| |_ 
     _|
  • Input 5

Output

     _   _   
    |_|_| |_ 
 _   _|    _|
|_|_|_       
  |_|_|      
    |_       
     _|      
  |_|        
  • Input 10

Output

           _       _                                           
         _|_|    _|_|                                          
        |_|_   _|_|_   _                                       
         _|_|_| |_| |_|_|                                      
   _    |_|_|_        |_                                       
 _|_|    _| |_|        _|                                      
|_|_   _|_          |_|                                        
 _|_|_|_|_|_                                                   
|_| |_|_|_|_|_                                                 
     _|_|_| |_|                                                
    |_| |_                                                     
         _|_   _   _           _   _           _   _           
   _    |_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_         
 _|_|    _|_|_|_|_| |_|    _   _|_| |_|    _   _|_| |_|        
|_|_   _|_|_|_|_|_        |_|_|_|_        |_|_|_|_             
 _|_|_|_|_|_|_|_|_|_   _   _|_|_|_|_   _   _|_|_|_|_   _   _   
|_| |_|_|_| |_|_|_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_ 
     _|_|    _|_|    _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|
    |_|     |_|     |_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_     
                         _|_|_|_|_|_|_|_|_|_|_| |_| |_|_|_|_   
                   _    |_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_ 
                 _|_|    _|_|_|_|_|_|_|_|_| |_|    _   _|_| |_|
                |_|_   _|_|_|_|_|_|_|_|_|_        |_|_|_|_     
                 _|_|_|_|_|_|_|_|_|_|_|_|_|_        |_| |_|    
                |_| |_|_|_| |_|_|_| |_|_|_|_|_                 
                     _|_|    _|_|    _|_|_| |_|                
                    |_|     |_|     |_| |_                     
                                         _|_   _   _           
                                   _    |_|_|_|_|_|_|_         
                                 _|_|    _|_|_|_|_| |_|        
                                |_|_   _|_|_|_|_|_             
                                 _|_|_|_|_|_|_|_|_|_   _   _   
                                |_| |_|_|_|_|_|_|_|_|_|_|_|_|_ 
                                     _|_|_|_|_|_|_|_|_|_|_| |_|
                                    |_| |_|_|_|_|_|_|_|_|_     
               _   _                     _|_|_| |_| |_|_|_|_   
              |_|_| |_             _    |_|_|_        |_|_|_|_ 
           _   _|    _|          _|_|    _| |_|    _   _|_| |_|
          |_|_|_                |_|_   _|_        |_|_|_|_     
            |_|_|                _|_|_|_|_|_        |_| |_|    
              |_   _       _    |_|_|_|_|_|_|_                 
           _   _|_|_|    _|_|    _|_|_|_|_| |_|                
          |_|_|_|_|_   _|_|_   _|_|_|_|_|_                     
            |_| |_| |_|_|_|_|_| |_| |_|_|_|_                   
                      |_|_|_|_        |_|_|_|_                 
                   _   _|_| |_|    _   _|_| |_|                
                  |_|_|_|_        |_|_|_|_                     
                    |_| |_|         |_| |_|                    
  • Input 12

Output

                                                               _   _           _   _                                           _   _           _   _                                           
                                                              |_|_|_|_        |_|_|_|_                                        |_|_|_|_        |_|_|_|_                                         
                                                           _   _|_| |_|    _   _|_| |_|                                    _   _|_| |_|    _   _|_| |_|                                        
                                                          |_|_|_|_        |_|_|_|_                                        |_|_|_|_        |_|_|_|_                                             
                                                            |_|_|_|_   _   _|_|_|_|_   _   _                                |_|_|_|_   _   _|_|_|_|_   _   _                                   
                                                              |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                 
                                                           _   _|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|                            _   _|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|                                
                                                          |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                     
                                               _   _        |_|_|_|_|_|_|_|_|_| |_| |_|_|_|_                   _   _        |_|_|_|_|_|_|_|_|_| |_| |_|_|_|_                                   
                                              |_|_|_|_        |_|_|_|_|_|_|_|_        |_|_|_|_                |_|_|_|_        |_|_|_|_|_|_|_|_        |_|_|_|_                                 
                                           _   _|_| |_|    _   _|_|_|_|_|_| |_|    _   _|_| |_|            _   _|_| |_|    _   _|_|_|_|_|_| |_|    _   _|_| |_|                                
                                          |_|_|_|_        |_|_|_|_|_|_|_|_        |_|_|_|_                |_|_|_|_        |_|_|_|_|_|_|_|_        |_|_|_|_                                     
                                            |_|_|_|_   _   _|_|_|_|_|_|_|_|_        |_| |_|                 |_|_|_|_   _   _|_|_|_|_|_|_|_|_        |_| |_|                                    
                                              |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                                 
                                           _   _|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|                            _   _|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|                                                
                                          |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_                                                     
                                            |_| |_| |_|_|_|_|_|_|_|_|_|_|_|_   _   _           _   _        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_   _   _           _   _           _   _           
                                                      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_         
                                                   _   _|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|    _   _|_| |_|    _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|    _   _|_| |_|    _   _|_| |_|        
                                                  |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_             
                                                    |_| |_| |_|_|_|_|_|_|_|_|_|_|_|_   _   _|_|_|_|_   _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_   _   _|_|_|_|_   _   _|_|_|_|_   _   _   
                                                              |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|_|_| |_|_|_|_|_|_|_|_|_|_|_| |_|_|_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_ 
                                                           _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|    _|_|    _|_|_|_|_|_|_|_|_|_|    _|_|    _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|
                                                          |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_    |_|     |_| |_|_|_|_|_|_|_|_    |_|     |_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_     
                                               _   _        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|_|_|_| |_| |_|_|_|_   
                                              |_|_|_|_        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|_           _    |_|_|_|_|_| |_|_           _    |_|_|_|_|_|_|_|_|_|_|_        |_|_|_|_ 
                                           _   _|_| |_|    _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|    _|_|        _|_|    _|_|_|_|    _|_|        _|_|    _|_|_|_|_|_|_|_|_| |_|    _   _|_| |_|
                                          |_|_|_|_        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_    |_|         |_|_   _|_|_|_|_    |_|         |_|_   _|_|_|_|_|_|_|_|_|_        |_|_|_|_     
                                            |_|_|_|_   _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|_|_|_|_|_|_        |_| |_|    
                                              |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_   _            |_| |_|_|_| |_|_                |_| |_|_|_| |_|_|_| |_|_|_|_|_                 
                                           _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                _|_|    _|_|                    _|_|    _|_|    _|_|_| |_|                
                                          |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_   _            |_|     |_|                     |_|     |_|     |_| |_                     
                                            |_| |_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                _|_   _   _           
                                                      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| |_|_                                                           _    |_|_|_|_|_|_|_         
                                                   _   _|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|    _|_|                                                        _|_|    _|_|_|_|_| |_|        
                                                  |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_    |_|                                                         |_|_   _|_|_|_|_|_             
                                                    |_| |_| |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                _|_|_|_|_|_|_|_|_|_   _   _   
           _       _                                          |_|_|_|_|_|_| |_|_|_| |_|_|_|_|_|_   _                                                            |_| |_|_|_|_|_|_|_|_|_|_|_|_|_ 
         _|_|    _|_|                                      _   _|_|_|_|_|    _|_|    _|_|_|_|_|_|_|_|                                                                _|_|_|_|_|_|_|_|_|_|_| |_|
        |_|_   _|_|_   _                                  |_|_|_|_|_|_|_    |_|     |_| |_|_|_|_|_|_   _                                                            |_| |_|_|_|_|_|_|_|_|_     
         _|_|_| |_| |_|_|                      _   _        |_|_|_|_|_|_|                _|_|_|_|_|_|_|_|                                      _   _                     _|_|_| |_| |_|_|_|_   
   _    |_|_|_        |_                      |_|_|_|_        |_|_| |_|_           _    |_|_|_|_|_| |_|_                                      |_|_| |_             _    |_|_|_        |_|_|_|_ 
 _|_|    _| |_|        _|                  _   _|_| |_|    _   _|    _|_|        _|_|    _|_|_|_|    _|_|                                  _   _|    _|          _|_|    _| |_|    _   _|_| |_|
|_|_   _|_          |_|                   |_|_|_|_        |_|_|_    |_|         |_|_   _|_|_|_|_    |_|                                   |_|_|_                |_|_   _|_        |_|_|_|_     
 _|_|_|_|_|_                                |_|_|_|_   _   _|_|_|                _|_|_|_|_|_|_|_|                                           |_|_|                _|_|_|_|_|_        |_| |_|    
|_| |_|_|_|_|_                                |_|_|_|_|_|_|_|_|_   _            |_| |_|_|_| |_|_                                              |_   _       _    |_|_|_|_|_|_|_                 
     _|_|_| |_|                            _   _|_|_|_|_|_|_|_|_|_|_|                _|_|    _|_|                                          _   _|_|_|    _|_|    _|_|_|_|_| |_|                
    |_| |_                                |_|_|_|_|_|_|_|_|_|_|_|_|_   _            |_|     |_|                                           |_|_|_|_|_   _|_|_   _|_|_|_|_|_                     
         _|_   _   _           _   _        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                   |_| |_| |_|_|_|_|_| |_| |_|_|_|_                   
   _    |_|_|_|_|_|_|_        |_|_|_|_        |_|_|_|_|_|_|_|_|_|_| |_|_                                                                              |_|_|_|_        |_|_|_|_                 
 _|_|    _|_|_|_|_| |_|    _   _|_| |_|    _   _|_|_|_|_|_|_|_|_|    _|_|                                                                          _   _|_| |_|    _   _|_| |_|                
|_|_   _|_|_|_|_|_        |_|_|_|_        |_|_|_|_|_|_|_|_|_|_|_    |_|                                                                           |_|_|_|_        |_|_|_|_                     
 _|_|_|_|_|_|_|_|_|_   _   _|_|_|_|_   _   _|_|_|_|_|_|_|_|_|_|_|                                                                                   |_| |_|         |_| |_|                    
|_| |_|_|_| |_|_|_| |_|_|_|_|_|_|_|_|_|_|_| |_|_|_| |_|_|_|_|_|_   _                                                                                                                           
     _|_|    _|_|    _|_|_|_|_|_|_|_|_|_|    _|_|    _|_|_|_|_|_|_|_|                                                                                                                          
    |_|     |_|     |_| |_|_|_|_|_|_|_|_    |_|     |_| |_|_|_|_|_|_   _                                                                                                                       
                         _|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|                                                                                                                      
                   _    |_|_|_|_|_| |_|_           _    |_|_|_|_|_| |_|_                                                                                                                       
                 _|_|    _|_|_|_|    _|_|        _|_|    _|_|_|_|    _|_|                                                                                                                      
                |_|_   _|_|_|_|_    |_|         |_|_   _|_|_|_|_    |_|                                                                                                                        
                 _|_|_|_|_|_|_|_|                _|_|_|_|_|_|_|_|                                                                                                                              
                |_| |_|_|_| |_|_                |_| |_|_|_| |_|_                                                                                                                               
                     _|_|    _|_|                    _|_|    _|_|                                                                                                                              
                    |_|     |_|                     |_|     |_|                                                                                                                                

Scoring

This is , so the shortest program in bytes wins.

\$\endgroup\$
8
  • \$\begingroup\$ I'm sure someone will complain about the vagueness of a 'huge amount of spaces', so how about an asymptotic bound? \$\endgroup\$
    – feersum
    Jul 12, 2015 at 9:52
  • 1
    \$\begingroup\$ @feersum Well I disallowed trailing spaces altogether, so nobody will complain now! \$\endgroup\$
    – Fatalize
    Jul 12, 2015 at 9:57
  • 2
    \$\begingroup\$ I'm complaining... now you're being a whitespace Nazi! \$\endgroup\$
    – feersum
    Jul 12, 2015 at 10:21
  • \$\begingroup\$ @feersum and you are a horizontal ellipsis Nazi! \$\endgroup\$
    – Optimizer
    Jul 12, 2015 at 12:35
  • \$\begingroup\$ This is the best fractal question ever, I hope I have time to participate! Is it ok to rotate the curve through 90,180,270 degrees or does it have to be displayed per the examples? \$\endgroup\$ Jul 12, 2015 at 13:00

5 Answers 5

10
\$\begingroup\$

Ruby, 239 201 bytes

This is a lambda function which should be called in the same manner as the one in the ungolfed version.

Golfing improvements include: assignment of 8<<n/2 to a variable for re-use; upto loop instead of each loop; ternary operator instead of if..else..end; use of [y,y+=d].max to calculate where to print the |; use of ?_ and ?| instead of the equivalent '|'and '_'; and elimination of redundant %4 (thanks Sp3000.)

->n{a=Array.new(m=8<<n/2){" "*m}
p=q=1+x=y=m/2
r=3
1.upto(1<<n){|i|d=(r&2)-1
r%2>0?(a[y][x+=d]=?_
x+=d):(a[[y,y+=d].max][x]=?|
p=x<p ?x:p
q=x>q ?x:q)
r+=i/(i&-i)}
a.delete(a[0])
puts a.map{|e|e[p..q]}}

It relies on the following formula from Wikipedia:

First, express n in the form k*(2^m) where k is an odd number. The direction of the nth turn is determined by k mod 4 i.e. the remainder left when k is divided by 4. If k mod 4 is 1 then the nth turn is R; if k mod 4 is 3 then the nth turn is L.

Wikipedia gives the following code:

There is a simple one line non-recursive method of implementing the above k mod 4 method of finding the turn direction in code. Treating turn n as a binary number, calculate the following boolean value: bool turn = (((n & −n) << 1) & n) != 0

I improved this to i/(i&-i)%4 which uses the same technique of using the expression i&-i to find the least significant digit but my expression gives 1 (for left turn)or 3 (for right turn) directly, which is handy as I track direction as a number 0..3 (in order north, west, south, east for golfing reasons.)

Ungolfed original in test program

f=->n{
  a=Array.new(8<<n/2){" "*(8<<n/2)}  #Make an array of strings of spaces of appropriate size 
  p=q=1+x=y=4<<n/2                   #set x&y to the middle of the array, p&q to the place where the underscore for n=0 will be printed.                             
  r=3                                #direction pointer, headed East
  (1..1<<n).each{|i|                 #all elements, starting at 1
    d=(r&2)-1                          #d is +1 for East and South, -1 for West and North
    if r%2>0                           #if horizontal
      a[y][x+=d]='_'                     #move cursor 1 position in direction d, print underscore,
      x+=d                               #and move again.
    else                               #else vertical
      a[(y+([d,0].max))][x]='|'          #draw | on the same line if d negative, line below if d positive
      y+=d                               #move cursor
      p=x<p ?x:p                         #update minimum and maximum x values for whitespace truncation later
      q=x>q ?x:q                         #(must be done for vertical bars, to avoid unnecesary space in n=0 case)
    end
    r=(r+i/(i&-i))%4                   #update direction
  }
  a.delete(a[0])                     #first line of a is blank. delete all blank lines.
  puts a.map!{|e|e[p..q]}                 #use p and q to truncate all strings to avoid unnecessary whitespace to left and right.
}


f.call(0)
f.call(2)
f.call(3)
f.call(11)
\$\endgroup\$
3
  • \$\begingroup\$ @Fatalize both functions are (currently) identical (except for the comments and whitespace.) I've added printing to stdout instead of returning a value (+5 bytes) and deleted the f= at the beginning as this is not normally counted for an anonymous function definition (-2 bytes.) More golfing tomorrow. Note that you will still have to execute the golfed function, by assigning it to a variable f=->n{.....} and calling it using f.call(n) as in the test program example. \$\endgroup\$ Jul 13, 2015 at 21:30
  • 1
    \$\begingroup\$ @Fatalize BTW I think the fractal looks absolutely awesome in my console. Thanks for the challenge. \$\endgroup\$ Jul 13, 2015 at 21:32
  • \$\begingroup\$ @Sp3000 indeed %4 is not necessary, as r is only used in the expressions r%2 and r&2. Thanks for the tip. I'm now down to 202. \$\endgroup\$ Jul 14, 2015 at 22:16
8
\$\begingroup\$

Python 2, 270 222 bytes

y=X=Y=0
i=m=x=1
D={}
k=2**input()
while~k+i:j=Y+(y>0);s={2*X+x};D[j]=D.get(j,s)|s;m=min(m,*s);Y+=y;X+=x;exec i/(i&-i)*"x,y=y,-x;";i+=1
for r in sorted(D):print"".join(" | _"[(n in D[r])+n%2*2]for n in range(m,max(D[r])+1))

Now using the formula for the nth turn. I saw the (((n & −n) << 1) & n) formula on Wikipedia, but didn't realise how useful it was until I saw it in @steveverrill's answer. I actually drop the %4 as well, so there's a lot of rotating going on, making larger inputs take a while.


Side remark: This isn't graphical output, but here's some golfed turtle code:

from turtle import*
for i in range(1,2**input()+1):fd(5);lt(i/(i&-i)*90)
\$\endgroup\$
2
  • \$\begingroup\$ As long as it doesn't take an hour to run, it's fine by me \$\endgroup\$
    – Fatalize
    Jul 12, 2015 at 15:14
  • \$\begingroup\$ If I understand correctly, your second code could be changed very slightly to become an answer to this challenge. \$\endgroup\$
    – nedla2004
    Nov 22, 2016 at 2:08
3
\$\begingroup\$

C#, 337 bytes

There's a bit of rules abuse here. There's no restriction on leading space. Unfortunately, the canvas is finite, so there is an upper limit for n.

Indented for clarity:

using C=System.Console;
class P{
    static void Main(string[]a){
        int n=int.Parse(a[0]),d=2,x=250,y=500;
        var f="0D";
        while(n-->0)
            f=f.Replace("D","d3t03").Replace("T","10d1t").ToUpper();
        C.SetBufferSize(999,999);
        foreach(var c in f){
            n=c&7;
            d=(d+n)%4;
            if(n<1){
                var b=d%2<1;
                x+=n=b?1-d:0;
                y+=b?0:2-d;
                C.SetCursorPosition(x*2-n,y+d/3);
                C.Write(b?'_':'|');
            }
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 65 64 bytesSBCS

('_|'⍴⍨≢a)@a⍴∘''⊃1+⌈/a←(⊢-⌊/)⌈2+/÷∘¯2 1¨11 9∘○¨+\0,(⊢,0j1×⌽)⍣⎕,1

Try it online!

(⊢,0j1×⌽)⍣⎕,1 generates a list of steps as complex numbers. It starts from 1 and repeatedly appends (,) a reversed () copy of the list multiplied by 0j1=sqrt(-1).

+\0, prepend 0 and compute prefix sums

11 9∘○¨ decompose complex into (real;imaginary) pairs

÷∘¯2 1¨ divide the real parts by -2

2+/ sums of adjacent pairs

ceiling

(⊢-⌊/) subtract the minima from all, so that coords are non-negative

a← assign to a

⍴∘''⊃1+⌈/ create an empty char matrix such that the max coords can fit

('_|'⍴⍨≢a)@a put alternating _ and | at the coordinates from a

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 220

Using the wikipedia formula for left and right turns.

n=>(d=>{for(i=x=y=d;i<1<<n;d+=++i/(i&-i))z=d&2,(w=d&1)?y+=z/2:x+=1-z,g=x<0?g.map(r=>[,,...r],x=1):g,g=y<0?[y=0,...g]:g,r=g[y]=g[y]||[],r[x]='_|'[w],w?y-=!z:x+=1-z})(0,g=[])||g.map(r=>[...r].map(c=>c||' ').join``).join`
`

Less golfed

n=>{
  g=[];
  for(i=x=y=d=0;i<1<<n;d+=++i/(i&-i))
    z=d&2,
    (w=d&1)?y+=z/2:x+=1-z,
    g=x<0?g.map(r=>[,,...r],x=1):g,
    g=y<0?[y=0,...g]:g,
    r=g[y]=g[y]||[],
    r[x]='_|'[w],
    w?y-=!z:x+=1-z
  return g.map(r=>[...r].map(c=>c||' ').join``).join`\n`
}

F=
n=>(d=>{for(i=x=y=d;i<1<<n;d+=++i/(i&-i))z=d&2,(w=d&1)?y+=z/2:x+=1-z,g=x<0?g.map(r=>[,,...r],x=1):g,g=y<0?[y=0,...g]:g,r=g[y]=g[y]||[],r[x]='_|'[w],w?y-=!z:x+=1-z})(0,g=[])||g.map(r=>[...r].map(c=>c||' ').join``).join`
`

function update() {
  var n=+I.value
  O.textContent=F(n)
}

update()
pre { font-size: 8px }
<input id=I value=5 type=number oninput='update()'><pre id=O></pre>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.