43
\$\begingroup\$

Write a program that takes in (via STDIN/command line) a non-negative integer N.

When N is 0, your program should print O (that's capital Oh, not zero).

When N is 1, your program should print

\|/
-O-
/|\

When N is 2 your program should print

\ | /
 \|/
--O--
 /|\
/ | \

When N is 3 your program should print

\  |  /
 \ | /
  \|/
---O---
  /|\
 / | \
/  |  \

For larger N, this pattern continues on in the same exact fashion. Each of the eight rays of the "sun" should be made of N of the appropriate -, |, /, or \ characters.

Details

  • Instead of a program, you may write a function that takes an integer. The function should print the sun design normally or return it as a string.

  • You must either

    • have no trailing spaces at all, or
    • only have enough trailing spaces so the pattern is a perfect (2N+1)*(2N+1) rectangle.
  • The output for any or all N may optionally have a trailing newline.

Scoring

The shortest code in bytes wins.

\$\endgroup\$
2
  • \$\begingroup\$ Is a leading newline allowed? Especially interesting for N=0. \$\endgroup\$
    – Jakube
    May 7, 2015 at 23:10
  • \$\begingroup\$ @Jakube No. Trailing only. \$\endgroup\$ May 7, 2015 at 23:12

39 Answers 39

1
2
1
\$\begingroup\$

JavaScript (ES6), 142 140 134 117 bytes

n=>(g=x=>x?`${t=` `[r=`repeat`](n-x--)}\\${s=` `[r](x)}|${s}/${t}
`+g(x):`-`[r](n))(n)+`O`+[...g(n)].reverse().join``

Try It

f=
n=>(g=x=>x?`${t=` `[r=`repeat`](n-x--)}\\${s=` `[r](x)}|${s}/${t}
`+g(x):`-`[r](n))(n)+`O`+[...g(n)].reverse().join``
i.addEventListener("input",_=>o.innerText=f(+i.value))
o.innerText=f(i.value=1)
<input id=i type=number><pre id=o>

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

Powershell, 108 97 93 bytes

filter f{try{$s=' '*--$_
"\$s|$s/"
$_|f|%{(" $_ ","-$_-")[$_-match'O']}
"/$s|$s\"}catch{'O'}}

Explanation:

  • the recursive algorithm throws an exception on $s=' '*--$_ when current number less then 1;
  • the output is a perfect rectangle with trailing spaces.

Less golfed test script:

filter f{
    try{
        $s=' '*--$_
        "\$s|$s/"
        $_|f|%{(" $_ ","-$_-")[$_-match'O']}
        "/$s|$s\"
    }catch{
        'O'
    }
}

@(

,(0,"O")
,(1,"\|/",
    "-O-",
    "/|\")
,(2,"\ | /",
    " \|/ ",
    "--O--",
    " /|\ ",
    "/ | \")
,(3,"\  |  /",
    " \ | / ",
    "  \|/  ",
    "---O---",
    "  /|\  ",
    " / | \ ",
    "/  |  \")

) | % {
    $n,$expected = $_
    $result = $n|f
    "$result"-eq"$expected"
    $result
}

Output:

True
O
True
\|/
-O-
/|\
True
\ | /
 \|/ 
--O--
 /|\ 
/ | \
True
\  |  /
 \ | / 
  \|/  
---O---
  /|\  
 / | \ 
/  |  \
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 12 5 bytes

P*⊕θO

-7 bytes thanks to @ASCII-only, by using the builtin :*.

Try it online (verbose) or try it online (pure).

Explanation:

Print the 8-armed lines with a length of the input+1 (the +1 is because the center is included), without moving the cursor position:

MultiPrint(:*, Incremented(q));
P*⊕θ

Then print the "O" at the current location (still in the center, because we haven't moved due to the MultiPrint):

Print("O");
O
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 5? \$\endgroup\$
    – ASCII-only
    Jan 26, 2019 at 23:44
  • \$\begingroup\$ @ASCII-only Thanks! Didn't even knew there was a :* builtin for this. \$\endgroup\$ Jan 27, 2019 at 12:01
0
\$\begingroup\$

Groovy - 113

f={n->s=2*n+1;(1..s).each{r->(1..s+1).each{c->print c==r?n+1==r?"O":"\\":c==s-r+1?"/":c==n+1?"|":c>s?"\n":" "}}}

I think the ternary logic could probably be cleaned up to give a shorter answer...

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 87 bytes

INPUT N
FOR Y=-N TO N
FOR X=-N TO N?" \/|-     O"[(X==Y)+!(X+Y)*2+!X*3+!Y*4];
NEXT?NEXT
\$\endgroup\$
0
\$\begingroup\$

Canvas, 14 bytes

╵⌐|*⁸/+↔ω↶n┼O╋

Try it here!

\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 99 bytes

.+
$* X$&$*-
 ( *)X(-+)
$1\|/¶$2X$2¶$1/|\
+`^ ( *.)( *)
$1$2 | $2/¶$&
+` ( */)( *).+$
$&¶$1$2 | $2\

Try it online! Explanation:

.+
$* X$&$*-

Place the X and draw n -s to its right. This completes the output for n=0.

 ( *)X(-+)
$1\|/¶$2X$2¶$1/|\

If n>0 then copy the -s to the left, and add the first level of \|/ and /|\ around the X. This completes the output for n=1.

+`^ ( *.)( *)
$1$2 | $2/¶$&

Extend the \|/ upwards, removing a space of indent each time, adding it back on each side of the |.

+` ( */)( *).+$
$&¶$1$2 | $2\

Extend the /|\ downwards, removing a space of indent each time, adding it back on each side of the |.

\$\endgroup\$
0
\$\begingroup\$

Vyxal, 46 bytes

(\\n›↳⁰↲\|\/n›↲⁰↳Ṡ,)\O-?-,(\/n›↲⁰↳\|\\n›↳⁰↲Ṡ,)

Try it Online!

A mess that I can't be bothered fixing, at least for now.

\$\endgroup\$
0
\$\begingroup\$

Japt, 26 25 bytes

Pub golfing so, as usual, could probably be shorter.

Æ"\\|/"¬qXçÃÔp'OiUç-¹·ê û

Try it

Æ"\\|/"¬qXçÃÔp'OiUç-¹·ê û     :Implicit input of integer U
Æ                             :Map each X in the range [0,U)
 "\\|/"¬                      :  Split the string "\|/"
        q                     :  Join with
         Xç                   :    X spaces
           Ã                  :End map
            Ô                 :Reverse
             p                :Push
              'Oi             :  "O" prepended with
                 Uç-          :    "-" repeated U times
                    ¹         :End push
                     ·        :Join with newlines
                      ê       :Palindromise
                        û     :Centre pad each line with spaces to the length of the longest
\$\endgroup\$
1
2

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.