Dibuja algunas estrellas ASCII

15

Escriba un programa o función que dibuje una estrella ASCII, dado el tamaño de los brazos como entrada.

Aquí hay una estrella de tamaño 1

_/\_
\  /
|/\|

Aquí hay una estrella de tamaño 2

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

Aquí hay una estrella de tamaño 3

     /\
    /  \
___/    \___
\          /
 \        /
  \      /
  |  /\  |
  | /  \ |
  |/    \|

Y así.

Entrada

Un número entero positivo solo en cualquier formato conveniente , n > 0.

Salida

Una representación de una estrella en el arte ASCII, siguiendo las reglas anteriores. Las nuevas líneas iniciales / finales u otros espacios en blanco son opcionales, siempre que los puntos se alineen adecuadamente.

Reglas

  • Un programa completo o una función son aceptables. Si es una función, puede devolver el resultado en lugar de imprimirlo.
  • Las lagunas estándar están prohibidas.
  • Este es el por lo que se aplican todas las reglas habituales de golf, y gana el código más corto (en bytes).
AdmBorkBork
fuente
66
¿Es solo mi pantalla o las estrellas se ven anormalmente altas y delgadas?
caird coinheringaahing
2
Estúpido ASCII y su falta de
sesgos
@cairdcoinheringaahing La fuente utilizada por SE no es cuadrada: hay un espacio en blanco significativo entre las líneas, lo que aumenta la distorsión.
AdmBorkBork

Respuestas:

12

Carbón , 20 17 bytes

-3 bytes gracias a Neil.

Nν↙ν↑↑ν↖ν ×_ν↗ν‖M

Pruébalo en línea! El enlace es a la versión detallada.

Estoy bastante contento con este golf, así que ...

Explicación

Nν                 take a number as input and store in ν
  ↙ν               print / ν times downwards to the left
    ↑              move up once
     ↑ν            print | ν times upwards
       ↖ν          print \ ν times upwards to the left
                   print a space
          ×_ν      print _ ν times
             ↗ν    print / ν times upwards to the right
               ‖M  reflect horizontally
     /\                           
    /  \    "No, this is Patrick!"
___/    \___                      
\   ☉ ☉    /                      
 \   𝐷    /                       
  \      /                        
  |  /\  |                        
  | /  \ |                        
  |/    \| 
totalmente humano
fuente
Dibujarlo de abajo hacia arriba ahorra 2 bytes.
Neil
Por alguna razón, tengo que incrementar el argumento para Polygon... ¿Estoy haciendo algo mal?
totalmente humano
Eso es porque Polygondeja el cursor en el último carácter, pero no estaba usandoPolygon ... de hecho tampoco estaba usando el mejor código, ahora tengo 17.
Neil
No sé si obtuve 17 de la misma manera que tú pero ... ¡Gracias!
totalmente humano
Lo suficientemente cerca De hecho lo tuve Move(:Right); for (n) Print("_");.
Neil
5

SOGL V0.12 , 27 24 bytes

╔*¹.╚№┼№.╝+ø┐.∙.1ž.╚┼+╬³

Pruébalo aquí!

Explicación:

╔*                        push a string with input amount of underscores
  ¹                       wrap that in an array
   .╚                     push a "/" diagonal of the size of the input (the top lines)
     №                    reverse vertically
      ┼                   add horizontally the underscores behind the array
       №                  reverse vertically back
        .╝+               below that add a "\" diagonal (middle lines)
           ø              push an empty string as the base of the vertical bars
            ┐.∙           get an array of "|" with the length of the input
               .1ž        at [input; 1] in the empty string insert that
                  .╚┼     horizontally append a "/" diagonal
                     +    add that below everything else
                      ╬³  palindromize horizontally
dzaima
fuente
4

Python 2 ,  166160157155152  bytes

los exec enfoque es exactamente el mismo recuento de bytes.

i=input();t,z=" \\";y=t*2
for k in range(i*3):s=k%i;o=i+~s;p=i+o;g="_ "[i>k+1]*p;print[g+"/"+y*k+z+g,t*s+z+y*p+"/",~-i*t+"|"+o*t+"/"+y*s+z+o*t+"|"][k/i]

Pruébalo en línea!

Guardado 3 bytes gracias a Jonathan Frech .

Sr. Xcoder
fuente
Puede guardar algunos bytes reemplazándolos t=" ";y,z=t*2,"\\"con t,z=" \\";y=t+t.
Jonathan Frech
@ JonathanFrech Gracias.
Sr. Xcoder
3

Java 8, 385 376 344 304 285 280 268 264 252 250 + 19 bytes

n -> {int s = 2 * n, w = 2 * s, e = n-1, i = 0, o, l [] [] = new int [n * 3] [w]; para (; i <n; l [i] [s + ~ i] = l [n + i] [w + ~ i] = l [o] [s + ~ i] = 47, l [i] [o] = l [o] [ o] = l [n + i] [i] = 92, l [e] [i] = l [e] [w - ++ i] = 95, l [o] [e] = l [o] [ s + n] = 124) o = s + i; para (int [] b: l) System.out.println (nueva cadena (b, 0, w) .replace (" ", ""));}

Pruébalo en línea!

Roberto Graham
fuente
Puede acortarlo en 10 bytes utilizando trucos bit a bit y moviendo todas sus declaraciones de enteros fuera de sus bucles ( 270 bytes )
Sr. Xcoder
Y puede guardar otros 2 bytes moviendo también el int o=exterior del bucle ( 268 bytes )
Sr. Xcoder
Puede guardar 4 bytes más al deshacerse de los corchetes for-loop y usar ++directamente en la última aparición de i, por ejemplo: 264 bytes .
Kevin Cruijssen
1
252 bytes
Nevay
@Nevay Muy inteligente, gracias
Roberto Graham
2

Mathematica, 189 bytes

n(
  s_±x_±y_:=s->Array[If[x==y,s," "]&,{n,n}];
  StringRiffle[Characters@{"_/\\_","\\  /","|/\\:"}/.
    {"_"±#±n,"|"±#2±n,":"±#2±1,"\\"±#±#2,"/"±(n-#+1)±#2," "±0±1}
    /.":"->"|"//ArrayFlatten,"
",""])

La línea 2 define el operador auxiliar ±, que se utiliza para hacer que la línea 4 evalúe para:

{"_"  -> Array[If[#1 == n,          "_", " "] &, {n, n}], 
 "|"  -> Array[If[#2 == n,          "|", " "] &, {n, n}], 
 ":"  -> Array[If[#2 == 1,          ":", " "] &, {n, n}], 
 "\\" -> Array[If[#1 == #2,         "\\"," "] &, {n, n}], 
 "/"  -> Array[If[1 + n - #1 == #2, "/", " "] &, {n, n}], 
 " "  -> Array[If[0 == 1,           " ", " "] &, {n, n}]}

En la línea 3, ReplaceAll( /.) toma una matriz que representa la estrella de tamaño 1, así como la lista de reglas anterior. Para los pasos finales, usamos ArrayFlatten, que es más corto que SubstitutionSystem, y StringRiffle.

hftf
fuente
2

Java 7, 295 bytes

La solución es el método f.

String s(String s,int n){while(n-->0)s=" "+s;return s;}String f(int x){String n="\n",s="/",b="\\",o="",u="_";int i=0;for(x--;i<x;u+="_")o+=s(s,2*x-i+1)+s(b,2*i++)+n;o+=u+s+s(b,2*i)+u+n;while(i>=0)o+=s(b,x-i)+s(s,4*x-2*(x+~i--))+n;while(i++<x)o+=s("|",x)+s(s,x-i)+s(b,2*i)+s("|",x-i)+n;return o;}

Pruébelo en línea (JDK 8)

Sin golf

String s(String s, int n) {
    while (n-- > 0)
        s = " " + s;
    return s;
}

String f(int x) {
    String
        n = "\n",
        s = "/",
        b = "\\",
        o = "",
        u = "_"
    ;
    int i = 0;
    for (x--; i < x; u += "_")
        o += s(s, 2*x - i + 1) + s(b, 2 * i++) + n;
    o += u + s + s(b, 2 * i) + u + n;
    while (i >= 0)
        o += s(b, x - i) + s(s, 4*x - 2*(x + ~i--)) + n;
    while (i++ < x)
        o += s("|", x) + s(s, x - i) + s(b, 2 * i) + s("|", x - i) + n;
    return o;
}

Expresiones de gratitud

  • -1 byte gracias a Kevin Cruijssen
Jakob
fuente
0

Python 2 , 137 bytes

n=input()
b,f,s,v='\/ |'
for i in range(3*n):u=i/~-n*n*'_';d=f+i%n*2*s+b;print[u+d+u,b+s*2*(3*n+~i)+f,v+d.center(2*n)+v][i/n].center(4*n)

Pruébalo en línea!

Lynn
fuente