12
\$\begingroup\$

Your task is to output a square sandbox of size n+2 with / characters in the corners and | for the sides, and - for the top, with n**2 layers of sand (.) overflowing out of the sandbox of width n+2

Testcases

1
->
/-/
|.|
/./
...

2
->
/--/
|..|
|..|
/../
....
....
....
....

3
->
/---/
|...|
|...|
|...|
/.../
.....
.....
.....
.....
.....
.....
.....
.....
.....

Trailing spaces and newlines are allowed

This is , so shortest code wins!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Why are there 10 layers of sand at the bottom for n=3? \$\endgroup\$
    – pxeger
    Mar 22 at 8:09
  • \$\begingroup\$ My bad that is a typo \$\endgroup\$
    – DialFrost
    Mar 22 at 8:10
  • \$\begingroup\$ Trailing newline is allowed, right? (you allow trailing spaces; not sure about trailing newline) \$\endgroup\$
    – ophact
    Mar 22 at 8:22
  • \$\begingroup\$ Challenge edited \$\endgroup\$
    – DialFrost
    Mar 22 at 8:33
  • 1
    \$\begingroup\$ @ophact Trailing whitespace is allowed by default for ascii-art challenges, so if the question doesn't specify it's ok \$\endgroup\$
    – pxeger
    Mar 22 at 9:20

14 Answers 14

5
\$\begingroup\$

Python 2, 81 bytes

n=input()
i=2
while~n*n<i:i-=1;d='/.|'[~-i*(i+n)and~(i<-n)];print d+'-.'[i<1]*n+d

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python, 69 bytes

lambda n:[f"/{'-'*n}/",*n*[f"|{(c:='.'*n)}|"],f"/{c}/",*n*n*[c+'..']]

Attempt This Online!

-2 bytes thanks to @ophact.

Outputs a list of lines, as allowed by standard I/O rules.

\$\endgroup\$
2
  • \$\begingroup\$ You can save 2 bytes as follows: ato.pxeger.com/… \$\endgroup\$
    – ophact
    Mar 22 at 8:14
  • \$\begingroup\$ Idk if this output format is allowed, but -1 byte \$\endgroup\$
    – emanresu A
    Mar 22 at 19:53
4
\$\begingroup\$

05AB1E (legacy), 30 29 bytes

'-×'/.ø©Ð„|.‡I.D®W'.:ÐW‡In.D»

Uses the legacy version of 05AB1E because it can take the minimum character of a string (based on its unicode value), saving 2 bytes over repushing the characters.

Try it online or verify all test cases.

Explanation:

'-×      '# Repeat "-" the (implicit) input amount of times as string
   '/.ø  '# Surround it with leading/trailing "/"
       ©  # Store this string in variable `®` (without popping)
Ð         # Triplicate it
 „|.‡     # Transliterate the "/" to "|" and "-" to "." in the other copy (the
          # transliterate ignores the trailing "…---/" since the strings are
          # of unequal length)
     I.D  # Duplicate it the input amount of times
®         # Push string `®`
 W        # Push the minimum (without popping): "-"
  '.:    '# Replace all "-" with "." in string `®`
Ð         # Triplicate it
 W        # Push the minimum (without popping): "."
  ‡       # Replace all "/" with "." (transliterate again ignores the trailing
          # "…---/")
   In     # Push the input, and square it
     .D   # Duplicate this string of "."s that many times
»         # Join all strings on the stack by newlines
          # (after which the result is output implicitly)
\$\endgroup\$
2
  • \$\begingroup\$ Wu... what so fast?? \$\endgroup\$
    – DialFrost
    Mar 22 at 7:49
  • 1
    \$\begingroup\$ @DialFrost I only saw your challenge little over 10 minutes ago, otherwise it might have been faster. ;p \$\endgroup\$ Mar 22 at 7:54
4
\$\begingroup\$

Excel, 96 95 bytes

=LET(r,REPT(".",A1),"/"&REPT("-",A1)&"/"&REPT("
|"&r&"|",A1)&"
/"&r&"/
"&REPT("."&r&".
",A1^2))

Screenshot

\$\endgroup\$
3
\$\begingroup\$

Java 11, 121 bytes

n->"/"+"-".repeat(n)+"/\n"+("|"+".".repeat(n)+"|\n").repeat(n)+"/"+".".repeat(n)+"/\n"+(".".repeat(n+2)+"\n").repeat(n*n)

Try it online.

Straight-forward approach using Java 11+'s String#repeat.

\$\endgroup\$
3
\$\begingroup\$

APL+WIN, 58 bytes

Prompts for n

o,(⍉(n,⍴c)⍴c←'-',((n+1)+n*2)⍴'.'),o←(1,n,1,(n←⎕)*2)/'/|/.'

Try it online! Thanks to Dyalog Classic

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

Charcoal, 19 bytes

Nθ↶UB.×.×θθ/F³«θ↶²/

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input n.

Change the default drawing direction to up. (I do it here because I can use the default pivot of 90°.)

UB.

Set the default character for unpainted areas to ..

×.×θθ

Draw the right-hand edge of the overflowing sand. (The rest of the sand gets drawn as part of the unpainted area.)

/

Draw the bottom right /.

F³«

Repeat three times.

θ

Draw the right |s, the top -s or the left |s as appropriate.

↶²

Pivot the default drawing direction by another 90°.

/

Draw the top right, top left or bottom right / as appropriate.

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

Retina, 42 bytes

.+
/$&*-/¶$&*$(|$&*.|¶)/$&*./$&**$(¶.$&*..

Try it online! Link includes test cases. Explanation: Straightforward application of Retina 1's string repeat operator, except that ** is shorthand for *$&* thus squaring the input.

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

JavaScript (Node.js), 77 bytes

n=>`/${(C=D=>D.repeat(n))('-')}/
${C(`|${a=C('.')}|
`)}/${a}/
`+C(C(a+`..
`))

Try it online!

Constructs a large template string using a function C to shorten the usage of repeat.

Thanks @Arnauld for -4 bytes by reminding me that literal linefeeds exist. (I've been doing way too much Python recently.)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 77 bytes with literal line feeds. \$\endgroup\$
    – Arnauld
    Mar 22 at 15:21
  • \$\begingroup\$ @Arnauld Wow, I've been doing too much Python so I forgot about literal line-feeds... \$\endgroup\$
    – ophact
    Mar 22 at 16:30
1
\$\begingroup\$

Rust, 140 134 bytes

|i|{let r=".".repeat(i);print!("/{}/
{}/{}/
{}
","-".repeat(i),format!("|{}|
",r).repeat(i),r,(".".repeat(i+2)+"
").repeat(i.pow(2)))}

Try it online!

To 134 bytes: I was able to save 6 bytes by using type hinting.

The link includes a header and a footer for calling the closure, and I call it for 1, 2, and 3, to prove that it works!

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

C (GCC), 143 137 bytes

saved 6 bytes thanks to @ceilingcat

j;main(n){scanf("%d",&n);char s[n+=2];for(;j<n;puts(s))memset(s,46-!j,n),*s=s[n-1]="/|"[j++&&j^n];*s=s[--n]=46;for(j=--n*n;j--;)puts(s);}

Try it online!

Indented version:

j;
main(n){
    scanf("%d",&n);
    char s[n+=2];
    for(;j<n;puts(s))
        memset(s,46-!j,n),
        *s=s[n-1]="/|"[j++&&j^n];
    *s=s[--n]=46;for(j=--n*n;j--;)puts(s);
}
\$\endgroup\$
1
1
\$\begingroup\$

Vyxal J, 32 bytes

¤-\/pǏD£‛/-‛|.Ŀ⁰Ḋ¥:g\.VDg\/$V⁰²Ḋ

Try it Online!

How?

¤-\/pǏD£‛/-‛|.Ŀ⁰Ḋ¥:g\.VDg\/$V⁰²Ḋ
¤                                # Push an empty string
 -                               # Push "-" * a + b (where a is the (implicit) input, and b is the empty string)
  \/p                            # Prepend "/"
     Ǐ                           # Append its first character (the slash)
      D                          # Triplicate
       £                         # Pop and store in the register
        ‛/-                      # Push string "/-"
           ‛|.                   # Push string "|." (for input 3, stack e.g. ["/---/", "/---/", "/-", "|."])
              Ŀ                  # Transliterate (stack e.g. ["/---/", "|...|"])
               ⁰Ḋ                # Duplicate the top value of the stack the input amount of times (stack e.g. ["/---/", "|...|", "|...|", "|...|"])
                 ¥               # Push contents of register (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/---/"])
                  :              # Duplicate (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/---/", "/---/"])
                   g             # Take the minimum by character code, which is "-" (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/---/", "-"])
                    \.V          # Replace with a period (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/.../"])
                       D         # Triplicate (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/.../", "/.../", "/.../"])
                        g        # Take the minimum by character code, which is "." (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/.../", "/.../", "."])
                         \/      # Push "/"
                           $     # Swap with the period (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/.../", "/.../", "/", "."])
                            V    # Replace (stack e.g. ["/---/", "|...|", "|...|", "|...|", "/.../", "....."])
                             ⁰²  # Push the input squared
                               Ḋ # Duplicate the top value of the stack that many times
                                 # J flag joins the stack by newlines
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 37 bytes

'.*'|▌'|+k(Ä_'-k*'/▌'/+_¬'.k⌠*k²(Ä_]n

Try it online.

Explanation:

'.*    '# Push a string with the (implicit) input amount of "."
'|▌    '# Prepend "|"
'|+    '# Append "|"
k(Ä     # Loop the input-1 amount of times,
        # using a single character as inner code-block:
   _    #  Duplicate the "|...|" string
'-k*   '# Push a string with the input amount of "-"
'/▌'/+  # Surround it with leading "/"
_       # Duplicate it
 ¬      # Rotate the items on the entire stack once
'.k⌠*  '# Push a string with the input+2 amount of "."
k²(Ä    # Loop input²-1 amount of times,
        #  using a single character as inner code-block:
    _   #  Duplicate the "." string
]       # Wrap the stack into a list
 n      # Join this list with newline delimiter
        # (after which the entire stack is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Perl 5 + -pl, 66 bytes

$;=c x$_;$_=a."-"x$_.ad."b$;bd"x$_.a.$;.a."dcc$;"x$_**2;y;a-d;/|..

Try it online!

Explanation

Generates the output, char by char, replacing characters used multiple times with bareword letters to avoid additional quotes or brackets. Makes use of operator precedence in that x**y works without quotes, but x*x doesn't. Also makes use of the fact that -p adds ;} to the code and uses ; as the separator for y///, saving another byte.

\$\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.