| Bytes | Lang | Time | Link |
|---|---|---|---|
| 130 | AWK | 250910T161129Z | xrs |
| 038 | MATL | 230301T231319Z | Luis Men |
| 032 | Vyxal J | 220423T191601Z | naffetS |
| 066 | Perl 5 + pl | 220412T083326Z | Dom Hast |
| 037 | MathGolf | 220412T074531Z | Kevin Cr |
| 137 | C GCC | 220322T145448Z | matteo_c |
| 095 | Excel | 220322T203410Z | Engineer |
| 134 | Rust | 220322T165342Z | Sylveste |
| 077 | JavaScript Node.js | 220322T095845Z | ophact |
| 019 | Charcoal | 220322T110928Z | Neil |
| 069 | Python | 220322T075919Z | pxeger |
| 042 | Retina | 220322T094746Z | Neil |
| 058 | APL+WIN | 220322T082005Z | Graham |
| 121 | Java 11 | 220322T080523Z | Kevin Cr |
| 029 | 05AB1E legacy | 220322T074801Z | Kevin Cr |
| 081 | Python 2 | 220322T075715Z | ovs |
AWK, 130 bytes
{for(;i++<$0^2+$0+2;print"")for(j=0;j++<y=$0+2;)printf 1~i&&1~j||1~i&&j~y||i~y&&1~j||i~y&&j~y?"/":1~i?"-":(1~j||j~y)&&i<y?"|":"."}
First attempt:
140 bytes
{for(s="/";i++<x=$0;)s=s"-"
for(s=s"/"RS;j++<x;)u=u"."
for(;k++<x;)s=s"|"u"|"RS
for(s=s"/"u"/"RS;m++<x+2;)w=w"."
for(;l++<x^2;)s=s w RS}$0=s
Vyxal J, 32 bytes
¤-\/pǏD£‛/-‛|.Ŀ⁰Ḋ¥:g\.VDg\/$V⁰²Ḋ
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
Perl 5 + -pl, 66 bytes
$;=c x$_;$_=a."-"x$_.ad."b$;bd"x$_.a.$;.a."dcc$;"x$_**2;y;a-d;/|..
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.
MathGolf, 37 bytes
'.*'|▌'|+k(Ä_'-k*'/▌'/+_¬'.k⌠*k²(Ä_]n
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)
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);}
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);
}
Excel, 96 95 bytes
=LET(r,REPT(".",A1),"/"&REPT("-",A1)&"/"&REPT("
|"&r&"|",A1)&"
/"&r&"/
"&REPT("."&r&".
",A1^2))
Rust, 140 134 bytes
|i|{let r=".".repeat(i);print!("/{}/
{}/{}/
{}
","-".repeat(i),format!("|{}|
",r).repeat(i),r,(".".repeat(i+2)+"
").repeat(i.pow(2)))}
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!
JavaScript (Node.js), 77 bytes
n=>`/${(C=D=>D.repeat(n))('-')}/
${C(`|${a=C('.')}|
`)}/${a}/
`+C(C(a+`..
`))
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.)
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.
Python, 69 bytes
lambda n:[f"/{'-'*n}/",*n*[f"|{(c:='.'*n)}|"],f"/{c}/",*n*n*[c+'..']]
-2 bytes thanks to @ophact.
Outputs a list of lines, as allowed by standard I/O rules.
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.
APL+WIN, 58 bytes
Prompts for n
o,(⍉(n,⍴c)⍴c←'-',((n+1)+n*2)⍴'.'),o←(1,n,1,(n←⎕)*2)/'/|/.'
Java 11, 121 bytes
n->"/"+"-".repeat(n)+"/\n"+("|"+".".repeat(n)+"|\n").repeat(n)+"/"+".".repeat(n)+"/\n"+(".".repeat(n+2)+"\n").repeat(n*n)
Straight-forward approach using Java 11+'s String#repeat.
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)
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
