16
\$\begingroup\$

Write a program or function that takes in a positive integer and prints or returns an ASCII art depiction of an archery arrow, scaled according to the input.

If the input is 1 the output is

  /\
 /  \
 \__/
  ||
  ||
  ||
  ||
  ||
  ||
  ||
  ||
 /||\
/ || \
/ || \
//  \\
/    \

If the input is 2 the output is

     /\
    /  \
   /    \
  /      \
  \      /
   \____/
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
    |  |
   /|  |\
  / |  | \
 /  |  |  \
/   |  |   \
/   |  |   \
/   |__|   \
/  /    \  \
/ /      \ \
//        \\
/          \

If the input is 3 the output is

        /\
       /  \
      /    \
     /      \
    /        \
   /          \
   \          /
    \        /
     \______/
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
      |    |
     /|    |\
    / |    | \
   /  |    |  \
  /   |    |   \
 /    |    |    \
/     |    |     \
/     |    |     \
/     |    |     \
/     |____|     \
/    /      \    \
/   /        \   \
/  /          \  \
/ /            \ \
//              \\
/                \

If the input is 4 the output is

           /\
          /  \
         /    \
        /      \
       /        \
      /          \
     /            \
    /              \
    \              /
     \            /
      \          /
       \________/
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
        |      |
       /|      |\
      / |      | \
     /  |      |  \
    /   |      |   \
   /    |      |    \
  /     |      |     \
 /      |      |      \
/       |      |       \
/       |      |       \
/       |      |       \
/       |      |       \
/       |______|       \
/      /        \      \
/     /          \     \
/    /            \    \
/   /              \   \
/  /                \  \
/ /                  \ \
//                    \\
/                      \

The pattern continues on for larger input numbers - the arrow is scaled up accordingly.

Notice that for any input N:

  • The arrowhead is a pentagon with 2×N underscores at the base, N slashes on each of the sides extending from the base, and 2×N slashes on the two sides that form the arrow tip.
  • The shaft (including the part within the fletching) consists of 11×N lines of two vertical bars separated by 2×(N-1) spaces. The very last line has underscores instead of spaces.
  • The fletching surrounds the lower 3×N lines of the shaft. Two lines of 2×N slashes extend diagonally downward from either side of the shaft and are joined with vertical columns of slashes.
  • The complete arrow is 16×N lines high and 6×N characters wide.

In your output there should be no empty lines before the arrow tip and only up to one trailing newline.

None of the output lines should have unnecessary leading spaces (the left edge of the fletching should be as leftmost as possible) but any of the lines may have any number of trailing spaces.

The shortest submission in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Is there a maximum input size? \$\endgroup\$ – Downgoat Aug 14 '15 at 3:41
  • 1
    \$\begingroup\$ @vihan No. Though you can assume the numbers wont overflow your memory/integers. \$\endgroup\$ – Calvin's Hobbies Aug 14 '15 at 4:29
  • \$\begingroup\$ Should we worry about text wrapping? For N>=14, the default window size on my terminal makes my arrow look not like an arrow. \$\endgroup\$ – kevinsa5 Aug 14 '15 at 4:41
  • \$\begingroup\$ @kevinsa5 No. Don't worry. \$\endgroup\$ – Calvin's Hobbies Aug 14 '15 at 4:43
2
\$\begingroup\$

Pyth, 149 147 146 144 146

Edit 2015-9-23: had a bug, fixed for 2 bytes. Might be possible to make shorter, I have no time now.

Still seems too long for a Pyth answer.

M*tGHK*11Q=Y*3QA" /"jbC+R*yQG+_Js[ms[*dGHgt-YdG\_gKG\_)tQs[gQGHgyQG\_*K\|)ms[*+QdGH*y-tQdG\\*+*8QydGHgYGH)Qms[*++KQdGHgYGH)tQ+g*13QG*hYH)mXd"\/")J

Explanation (almost up to date)

I basically generate the transpose of the arrow, since its line format is much simpler. Also, I only generate one half and then reverse it to save a lot of bytes.

M*tGH                                                       g(a,b) = (a - 1) * b
A" /"                                                       G = " ", H = "/"
              ms[...)tQ                                     N-1 lines inside body
                       s[...)                               1 line of edge of body
                             ms[...)Q                       N lines of tip + fin
                                     m...Q                  N-1 lines of fin
                                          +...              1 line of edge of fin
            s[                                )             flatten the maps to it
           J                                                save to J
          _                                                 reverse
                                                       J    another copy (unreversed)
                                               mXd"\/")     swap "\" and "/" on each line
         +                                                  add to other half
   +R*yQG                                                   add 2*N spaces to each line
  C                                                         transpose
jb                                                          join by newline

m                             tQ       map d over 0...N-2
 s[                          )         concatenate
   *dG                                 d spaces
      H                                "/"
          =Y*3Q                        save 3*N to Y
       gt-     dG                      3*N-d-2 spaces
                 \_                    "_"
                    K*11Q              save 11*N to K
                   g     G             11*N-1 spaces
                          \_           "_"

s[              )                      concatenate
  gQG                                  N-1 spaces
     H                                 "/"
      gyQG                             2*N-1 spaces
          \_                           "_"
            *K\|                       11*N pipes

m                               Q      map d over 0...N-1
 s[                            )       concatenate
   *+QdG                               N+d spaces
        H                              "/"
         *y-tQdG                       2*(N-d-1) spaces
                \\                     "\"
                  *+*8QydG             8*N+2*d spaces
                          H            "/"
                           gYG         3*N-1 spaces
                              H        "/"

m               tQ                     map d over 0...N-2
 +++                                   concatenate
    *++KQdG                            12*N+d spaces
           H                           "/"
            gYG                        3*N-1 spaces
               H                       "/"

+                                      concatenate
 g*13QG                                13*N-1 spaces
       *hYH                            3*N+1 slashes
\$\endgroup\$
3
\$\begingroup\$

Perl, 298 bytes

for$y(0..($w=<>)*16-1){$r=$w*3;for$x(0..$w*6-1){$z=$x<$r?$x:$w*6-$x-1,$_.=$z==$r-$y-1&&$y<$w*2||!$z&&$y>=$w*13-1||($z==$w*16-1-$y||$z==$w*13-1-$y)&&$z<$w*2?$x<$r?'/':'\\':$z==$y-$w&&$y>=$w*2&&$y<$r?$x<$r?'\\':'/':$z==$r-$w&&$y>=$r&&$y<$w*14?'|':$z>=$r-$w&&($y==$r-1||$y==$w*14-1)?_:$"}$_.=$/}print

Multi-line:

for$y(0..($w=<>)*16-1){$r=$w*3;  # read input and loop over rows
    for$x(0..$w*6-1){  # loop over columns

        # flip x on right side of arrow:
        $z=$x<$r?$x:$w*6-$x-1;

        # slashes on fletching and tip of arrowhead:
        $_.=$z==$r-$y-1&&$y<$w*2||!$z&&$y>=$w*13-1||($z==$w*16-1-$y||$z==$w*13-1-$y)&&$z<$w*2?$x<$r?'/':'\\':

        # slashes on underside of arrowhead:
        $z==$y-$w&&$y>=$w*2&&$y<$r?$x<$r?'\\':'/':

        # vertical bars on side of shaft:
        $z==$r-$w&&$y>=$r&&$y<$w*14?'|':

        # underscores on base of arrowhead and shaft:
        $z>=$r-$w&&($y==$r-1||$y==$w*14-1)?_:$"
   }
   $_.=$/  # add newline at end of each row
}
print  # print out string

Output:

2
     /\     
    /  \    
   /    \   
  /      \  
  \      /  
   \____/   
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
    |  |    
   /|  |\   
  / |  | \  
 /  |  |  \ 
/   |  |   \
/   |  |   \
/   |__|   \
/  /    \  \
/ /      \ \
//        \\
/          \

Thanks to Dom Hastings for helping me save 5 (and counting) bytes

\$\endgroup\$
  • 1
    \$\begingroup\$ Hey samgak, you can save a few bytes by moving the $w=<> declaration to the first time you use it like for$y(0..($w=<>)*16-1){ and a lot of the brackets can be removed (some do change the output, but I think you're going through those anyway!). Also your '_' can just be a literal _. Hope that helps! \$\endgroup\$ – Dom Hastings Aug 14 '15 at 5:44
2
\$\begingroup\$

Julia, 452 bytes

This answer isn't very inspired, and I haven't golfed it beyond the glaringly obvious. I intend for this to be a score for other people to beat.

function R(c,x,y,e,f,n)
for i=1:n
a[y,x]=c
x+=e
y+=f
end
end
function f(n::Int)
global a=fill(' ',16n,6n)
F='/'
B='\\'
q=3n+1
r=2n+1
R(F,3n,1,-1,1,2n)
R(B,q,1,1,1,2n)
R(B,n+1,r,1,1,n)
R(F,5n,r,-1,1,n)
R('_',r,3n,1,0,2n)
R('|',r,q,0,1,11n)
R('|',4n,q,0,1,11n)
R('_',r+1,14n,1,0,r-3)
R(F,2n,11n+1,-1,1,2n)
R(B,4n+1,11n+1,1,1,2n)
R(F,1,13n,0,1,3n)
R(B,6n,13n,0,1,3n)
R(F,2n,14n+1,-1,1,2n)
R(B,4n+1,14n+1,1,1,2n)
for b = 1:16n
println(join(a[b,:]))
end
end

Try it out:

julia> f(1)
  /\                          
 /  \                         
 \__/                         
  ||                          
  ||                          
  ||                          
  ||                          
  ||                          
  ||                          
  ||                          
  ||                          
 /||\                         
/ || \                        
/ || \                        
//  \\                        
/    \

ungolfed:

# Repeat a character through array a from (x,y) -> (x+n*dx,y+n*dy)
function R(a::Array{Char,2},c::Char, x::Int, y::Int, dx::Int, dy::Int, n::Int)
    for i = 1:n
        a[y,x] = c
        x += dx
        y += dy
    end
end

function arrow(n::Int)
    a = fill(' ',16n,6n)
    # upper left of head
    R(a,'/',3n,1,-1,1,2n)
    # upper right of head
    R(a,'\\',3n+1,1,1,1,2n)
    # lower left of head
    R(a,'\\',n+1,2n+1,1,1,n)
    # lower right of head
    R(a,'/',5n,2n+1,-1,1,n)
    # bottom of head
    R(a,'_',2n+1,3n,1,0,2n)
    # shaft
    R(a,'|',2n+1,3n+1,0,1,11n)
    R(a,'|',4n,3n+1,0,1,11n)
    R(a,'_',2n+2,14n,1,0,2n-2)
    # upper fletching edges
    R(a,'/',2n,11n + 1,-1,1,2n)
    R(a,'\\',4n+1,11n+1,1,1,2n)
    # fletching sides
    R(a,'/',1,13n,0,1,3n)
    R(a,'\\',6n,13n,0,1,3n)
    # lower fletching edges
    R(a,'/',2n,14n + 1,-1,1,2n)
    R(a,'\\',4n+1,14n+1,1,1,2n)
    for r = 1:16n
        println(join(a[r,:]))
    end
end
\$\endgroup\$
2
\$\begingroup\$

Ruby, 233

->n{(16*n).times{|i|j=i/n
m=n*3
e=' '*6*n
i%(11*n)==m-1&&e[2*n..4*n-1]=?_*2*n
j%14>2&&e[m+n-1]=e[m-n]=?|
k=(i-n)%m
k<n||j<11||(e[m+k]=?\\;e[m-1-k]=?/)
j>12&&(e[-1]=?\\;e[0]=?/)
j<3&&(e[n+l=(i-n*2)%(n*4)]=?\\;e[5*n-l-1]=?/)  
puts e}}

I am particularly pleased with the wraparound technique I used to draw all \ in the head with a single expression (and likewise for /)

Ungolfed in test program

f=->n{
  #loop line by line, 0..16*n-1
  (16*n).times{|i|

  #convenience variables
  j=i/n
  m=n*3

  #setup a string of spaces
  e=' '*6*n

  #draw the ___ at top and bottom of shaft
  i%(11*n)==m-1&&e[2*n..4*n-1]=?_*2*n
  #draw the |, overwriting the _ in the bottom corners
  j%14>2&&e[m+n-1]=e[m-n]=?|

  #draw the diagonal fletching
  k=(i-n)%m
  k<n||j<11||(e[m+k]=?\\;e[m-1-k]=?/)

  #draw the ends of the fletching (note index -1, meaning last character in string)
  j>12&&(e[-1]=?\\;e[0]=?/)

  #draw the arrow head, wrapping round with %(n*4)
  j<3&&(e[n+l=(i-n*2)%(n*4)]=?\\;e[5*n-l-1]=?/)

  #output the line  
  puts e}
}

f.call(gets.to_i)
\$\endgroup\$

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.