15
\$\begingroup\$

Write a program or function that draws an ASCII star, given the size of the arms as input.

Here's a star of size 1

_/\_
\  /
|/\|

Here's a star of size 2

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

Here's a star of size 3

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

And so on.

Input

A single positive integer in any convenient format, n > 0.

Output

An ASCII-art representation of a star, following the above rules. Leading/trailing newlines or other whitespace are optional, provided that the points line up appropriately.

Rules

  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
3
  • 7
    \$\begingroup\$ Is it just my screen, or do the stars look abnormally tall and thin? \$\endgroup\$ Sep 2, 2017 at 2:23
  • 3
    \$\begingroup\$ Stupid ASCII and its lack of shallow slants... \$\endgroup\$ Sep 2, 2017 at 19:19
  • \$\begingroup\$ @cairdcoinheringaahing The font used by SE isn't square - there's significant whitespace between lines, which increases the distortion. \$\endgroup\$ Sep 5, 2017 at 12:23

7 Answers 7

12
\$\begingroup\$

Charcoal, 20 17 bytes

-3 bytes thanks to Neil.

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

Try it online! Link is to verbose version.

I'm pretty happy with this golf so...

Explanation

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!"
___/    \___                      
\   ☉ ☉    /                      
 \   𝐷    /                       
  \      /                        
  |  /\  |                        
  | /  \ |                        
  |/    \| 
\$\endgroup\$
5
  • \$\begingroup\$ Drawing it bottom to top saves 2 bytes. \$\endgroup\$
    – Neil
    Sep 1, 2017 at 20:54
  • \$\begingroup\$ For some reason, I have to increment the argument to Polygon... Am I doing something wrong? \$\endgroup\$ Sep 1, 2017 at 21:08
  • \$\begingroup\$ That's because Polygon leaves the cursor on the last character, but I wasn't using Polygon... in fact I wasn't using the best code either, I'm down to 17 now. \$\endgroup\$
    – Neil
    Sep 1, 2017 at 23:06
  • \$\begingroup\$ I dunno if if I got 17 the same way you did but... Thanks! \$\endgroup\$ Sep 2, 2017 at 0:58
  • \$\begingroup\$ Near enough. I actually had Move(:Right); for (n) Print("_");. \$\endgroup\$
    – Neil
    Sep 2, 2017 at 10:01
5
\$\begingroup\$

SOGL V0.12, 27 24 bytes

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

Try it Here!

Explanation:

╔*                        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
\$\endgroup\$
4
\$\begingroup\$

Python 2,  166 160 157 155  152 bytes

The exec approach is exactly the same byte count.

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]

Try it online!

Saved 3 bytes thanks to Jonathan Frech.

\$\endgroup\$
2
  • \$\begingroup\$ You could save some bytes by replacing t=" ";y,z=t*2,"\\" with t,z=" \\";y=t+t. \$\endgroup\$ Sep 2, 2017 at 1:13
  • \$\begingroup\$ @JonathanFrech Thank you. \$\endgroup\$
    – Mr. Xcoder
    Sep 2, 2017 at 6:57
3
\$\begingroup\$

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];for(;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;for(int[]b:l)System.out.println(new String(b,0,w).replace("�"," "));}

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ You can shorten it by 10 bytes using bitwise tricks and moving all your integer declarations outside your loops (270 bytes) \$\endgroup\$
    – Mr. Xcoder
    Sep 3, 2017 at 17:49
  • \$\begingroup\$ And you can save yet another 2 bytes by moving the int o= outside the loop too (268 bytes) \$\endgroup\$
    – Mr. Xcoder
    Sep 3, 2017 at 17:51
  • \$\begingroup\$ You can save 4 more bytes by getting rid of the for-loop brackets and using ++ directly on the last occurrence of i, like this: 264 bytes. \$\endgroup\$ Sep 4, 2017 at 8:54
  • 1
    \$\begingroup\$ 252 bytes \$\endgroup\$
    – Nevay
    Sep 4, 2017 at 13:52
  • \$\begingroup\$ @Nevay Very clever, thank you \$\endgroup\$ Sep 4, 2017 at 14:22
2
\$\begingroup\$

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,"
",""])

Line 2 defines the helper operator ±, which is used to make line 4 evaluate to:

{"_"  -> 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}]}

In line 3, the ReplaceAll (/.) takes a matrix representing the star of size 1 as well as the list of rules above. For the final steps, we use ArrayFlatten, which is shorter than SubstitutionSystem, and StringRiffle.

\$\endgroup\$
2
\$\begingroup\$

Java 7, 295 bytes

Solution is method 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;}

Try It Online (JDK 8)

Ungolfed

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

Acknowledgments

  • -1 byte thanks to Kevin Cruijssen
\$\endgroup\$
1
0
\$\begingroup\$

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)

Try it online!

\$\endgroup\$

Your Answer

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

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