Frecce di tiro con l'arco di arte ASCII


16

Scrivi un programma o una funzione che accetta un numero intero positivo e stampa o restituisce una rappresentazione di arte ASCII di una freccia di tiro con l' arco , ridimensionata in base all'input.

Se l'ingresso è 1l'uscita è

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

Se l'ingresso è 2l'uscita è

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

Se l'ingresso è 3l'uscita è

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

Se l'ingresso è 4l'uscita è

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

Il modello continua per numeri di input più grandi: la freccia viene ridimensionata di conseguenza.

Si noti che per qualsiasi input N:

  • La punta della freccia è un pentagono con 2 × N sottolineature alla base, N barre su ciascuno dei lati che si estendono dalla base e 2 × N barre sui due lati che formano la punta della freccia.
  • L'albero (inclusa la parte all'interno del fletching) è costituito da 11 × N linee di due barre verticali separate da 2 × (N-1) spazi. L'ultima riga presenta caratteri di sottolineatura anziché spazi.
  • Il fletching circonda le linee 3 × N inferiori dell'albero. Due linee di barre 2 × N si estendono diagonalmente verso il basso da entrambi i lati dell'albero e sono unite con colonne verticali di barre.
  • La freccia completa è alta 16 × N e larga 6 × N.

Nel tuo output non dovrebbero esserci linee vuote prima della punta della freccia e solo fino a una nuova riga finale.

Nessuna delle linee di output dovrebbe avere spazi iniziali non necessari (il bordo sinistro del fletching dovrebbe essere il più a sinistra possibile) ma qualsiasi linea può avere un numero qualsiasi di spazi finali.

Vince l'invio più breve in byte.


C'è una dimensione massima di input?
Downgoat,

1
@vihan No. Anche se puoi presumere che i numeri non trabocchino la tua memoria / numeri interi.
Hobby di Calvin il

Dovremmo preoccuparci del ritorno a capo del testo? Per N> = 14, la dimensione della finestra predefinita sul mio terminale rende la mia freccia non simile a una freccia.
kevinsa5,

@ kevinsa5 No. Non preoccuparti.
Hobby di Calvin il

Risposte:


2

Pyth, 149 147 146 144 146

Modifica 23-9-2015: aveva un bug, corretto per 2 byte. Potrebbe essere possibile abbreviare, non ho tempo ora.

Sembra ancora troppo lungo per una risposta Pyth.

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

Spiegazione (quasi aggiornata)

Fondamentalmente ho generato la trasposizione della freccia, poiché il suo formato di linea è molto più semplice. Inoltre, ne generi solo una metà e quindi lo inverto per salvare molti byte.

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

3

Perl, 298 byte

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

Produzione:

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

Grazie a Dom Hastings per avermi aiutato a salvare 5 (e contando) byte


1
Ehi Samgak, puoi salvare qualche byte spostando la $w=<>dichiarazione la prima volta che la usi come piace for$y(0..($w=<>)*16-1){e molte parentesi possono essere rimosse (alcune cambiano l'output, ma penso che le stai comunque provando!). Anche il tuo '_'può essere solo un letterale _. Spero possa aiutare!
Dom Hastings,

2

Julia, 452 byte

Questa risposta non è molto ispirata e non l'ho giocata oltre l'evidentemente evidente. Intendo che questo sia un punteggio da battere per altre persone.

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

Provalo:

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

2

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

Sono particolarmente soddisfatto della tecnica avvolgente che ho usato per disegnare tutto \ nella testa con una sola espressione (e anche per /)

Non registrato nel programma di test

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)
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.