New answers tagged

0

Charcoal, 17 bytes UONN‽γJNN‽Φγ¬⁼ιKK Try it online! Link is to verbose version of code. Explanation: UONN‽γ Draw a rectangle of the given width and height using a random printable ASCII character. JNN Jump to the 0-indexed position of the needle. ‽Φγ¬⁼ιKK Print a random printable ASCII character, but filtering out the character under the cursor.


0

Perl 6, 57 bytes {(' '..'~').pick(2)[0 xx$^a*$^b-1,1;*].pick(*).rotor($a)} Try it online!


0

Ruby, 62 bytes ->w,h,x,y{a,b=[*?\s..?~].sample 2;r=[a*w]*h*$/;r[y*-~w+x]=b;r} Try it online!


0

Python 3, 137 bytes lambda w,h,x,y,c=chr:"\n".join([c(a)*w,c(a)*x+c(n)+c(a)*(w-x-1)][r==y]for r in range(h));a,n=__import__("random").sample(range(32,127),2) Try it online! Uses zero-based indexing for the needle position.


0

Icon, 119 bytes procedure f(w,h,x,y) until m:=char(32+?95)&n:=char(32+?95)&m~==n t:=[];1to h&put(t,repl(m,w))&\z t[y,x]:=n return t end Try it online! xand yare 1-indexed


0

C (clang), 130 117 116 109 bytes c[2];f(w,h,x,y){x+=y*w;for(h*=++w;y++<h|*c==c[1];)c[y%2]=time(0)%94+33;for(;h--;)printf(h%w?x--?c:c+1:"\n");} Try it online! Saved 13 thanks to @ceilingcat Didn't thought using just only time() was enough And also didn't thought printf accepted a int pointer! Needle is 0 indexed c[2]; // needle and hay stored in ...


1

Excel VBA, 128 118 bytes Sub n(w,h,x,y) a=Chr(95*Rnd+32) Do b=Chr(95*Rnd+32) Loop While b=a Range(Cells(1,1),Cells(h,w))=a Cells(y,x)=b End Sub Input and output are one-indexed. Output is to the top left cell range of the active sheet. Example output: I wish I could think of a shorter way to generate unique, random ASCII characters.


0

Octave with Statistics Package, 45 bytes @(x)randsample(' ':'~',2)(sparse(x{:},0:1)+1) The input is a cell array of 2 numeric vectors of length 2: {[h y] [w x]}, with x,y 1-based. Try it online!


0

C# (Visual C# Interactive Compiler), 145 bytes (a,b,c,d)=>{int l=0;for(var j=Enumerable.Range(32,95).OrderBy(k=>Guid.NewGuid()).ToList();l<a*b;)Write((char)j[l==c*a+d?1:0]+(++l%a<1?"\n":""));} Try it online!


3

PowerShell v6, 102 96 bytes param($x,$y,$a,$b)$j,$k=' '..'~'|random -C 2 -join((,$j*$x+++' ')*$y|%{"$_$k"[$i++-eq$b*$x+$a]}) Try it online! This answer, thanks to mazzy, uses the character range feature added in v6 to save several bytes over the more flexible answer below. PowerShell, 119..105 102 bytes -16 bytes thanks to mazzy -1 byte thanks to ...


0

ink, 55 33 55 bytes =l(n) ~temp i=2 {n-1:C}EC<> -(k){i<n: ~i++ C<>->k }->-> Try it online! Starts by printing C if input is not 1, then EC, then prints C another n-2 times.


1

Jelly, 14 bytes p/U⁼€ịØṖẊ¤s⁸Ḣ¤ Try it online! or see a test suite! Returns a list of lines. The last line in the Footer section displays it as a square. Takes input as [w, h] and [x, y], where x and y are 0-indexed. How it works p/U⁼€ịØṖẊ¤s⁸Ḣ¤ - Main link. Takes [w, h] on the left and [x, y] on the right p/ - Reduce Cartesian product over ...


1

J, 21 bytes u:@(32+2?95){~#.=i.@[ Try it online! Inspired by dzaima's APL answer. Thanks to Galen and Adam for the randomize fix.


0

SOGL, 18 bytes ~Δψ] ~Δψ⁴⁴=}A*∙až Try it here! 1-indexed.


1

Julia 1.0, 71 bytes f(w,h,x,y)=(z=unique(rand(' ':'~',9));g=fill(z[1],(h,w));g[y,x]=z[2];g) x,y are one indexed. Returns a 2d array of characters, I spent the extra bytes to make sure the default printing has the right orientation (' for transpose no longer works because they stick to a strict mathematical meaning). Getting two distinct randoms is ...


4

JavaScript (Node.js),  122  120 bytes Recursively builds the output character by character. The coordinates are 0-indexed. (w,h,x,y)=>(c=(r=_=>C=Math.random()*95+32|0)(),g=X=>Y<h?r()-c?Buffer([X-w?x-X|y-Y?c:C:13])+g(X<w?X+1:!++Y):g(X):'')(Y=0) Try it online! Commented (w, h, x, y) => ( // w = width, h = height, (x, y) = ...


2

Dyalog APL, 24 23 bytes f←{⎕UCS(32+2?95)[⍺∘≡¨⍳⍵]} Try it online! 0-indexed. -1 thanks to ngn


1

Perl 5, 86 bytes ($,,$b)=keys%{{map{chr,1}33..126}};$_=($,x$F[0].$/)x$F[1];s;(.* ){$F[3]}.{$F[2]}\K.;$b Try it online!


0

Add++, 65 bytes D,g,?!, L,c95Rdb[€BXd¦=BFV#@G+b[95€Ω%31€+€CA€RbU‽g$€=$€Ω:A$pbUp$T Try it online! Takes input as [y x] and [w h]. x and y are 1-indexed. Outputs a list of lines. Added 17 bytes to make sure the needle is distinct. How it works We start by generating two ranges from \$1\$ to \$95\$ inclusive, before choosing a random value from each. If ...


2

Python 2, 134 133 120 112 111 bytes lambda w,h,x,y:zip(*[iter(chr(r[x+y*w==i]+32)for i in range(w*h))]*w) from random import* r=sample(range(95),2) Try it online!


0

PHP, 113 109 bytes for([,$w,$h,$x,$y]=$argv,$a=range(' ','~'),shuffle($a);$i++<$h;print" ")for(;$$i++<$w;)echo$a[$i-$y||$$i-$x]; Try it online! Pass inputs as command arguments ($argv) in order of w, h, x and y. The x and y are one-indexed. for( [,$w,$h,$x,$y]=$argv, // put inputs from $argv array into 4 short named variables $a=range(' ','~'), ...


2

MarioLANG, 3287 Bytes I had to do this for the theme. +++++++++++++++++++++++++++++++++++++++++++++)++++++++++++++++++++++++++++++++)+++++++++++++++++++++++++++++++++++)++++++++++++++++++++++++++++++++++++++++++++++)++++++++++)++++++++++++++++++++++++++++++++++++++++++((((...............))))...........(.(((...............)....)....(.).).(((.............).)....


6

JavaScript (ES8),  322 282 278  272 bytes Builds the output line by line. f=(n,y=0,Y=(k=y>2*n)?4*n-y:y,S='___/_25__,/\\ \\25,\\/____/14_/,\\ 14,\\/____/,_____, \\____\\, / /, \\ \\,_\\, /, \\'.split`,`)=>~Y?(y-2*n?''.padEnd(n*5-(Y>>1)*5-3-y%2-k)+S[y?Y?k*2+y%2:4:5]:'/ 03').replace(/\d/g,n=>S[+n+6].repeat(Y-(n>2||-k)>&...


7

Charcoal, 70 69 bytes NθFθ«J⊕×⁷⊕ι⁰≔⊗⊕ιι↙ι↑←×⁵_P↖²→↗ι×⁴_↖ι←×⁵_↓P↙²→↘ιJ⁻×⁹θ⊗ι⊖ι__↗ι←P←__↖ι←__ Try it online! Link is to verbose version of code. Explanation: NθFθ« Input N and loop that many times. J⊕×⁷⊕ι⁰ Jump to the (middle) right corner of the front slice. ≔⊗⊕ιι Get the size of the slice. ↙ι↑←×⁵_P↖²→↗ι×⁴_↖ι←×⁵_↓P↙²→↘ι Draw the front slice. J⁻×⁹θ⊗...


0

Python 3, 119 bytes lambda n:(3*f"%s%s{n}\n%s %s\n")[:30]%(*[n if i//(2**n)%2else" "for i in[1021,1005,881,927,893,892,325,1019,877,877]],) Try it online! Each 'pixel' is represented as an integer where each power of two indicates whether the pixel is solid. This takes advantage of the fact that the three right-hand corners are always the digit.


0

Perl 5, 59 bytes $_--;say$"x$_."\\"for(0..$_);say$"x$_."/"for(reverse 0..$_) Example execution: perl -nE '$_--;say$"x$_."\\"for(0..$_);say$"x$_."/"for(reverse 0..$_)' Waits for an integer to be provided on STDIN when executed. Once provided, it will display the arrow.


1

Python 3, 55 bytes f=lambda n,i="":n and i+"\\\n"+f(n-1,i+" ")+i+"/\n"or"" Try it online!


0

Forth (gforth), 54 bytes : f for i spaces i for i 65 + emit next cr next ; 25 f Try it online! How it works : f ( n -- ) \ Print n+1 lines of searchlight. for \ loop from n to 0 inclusive [i] i spaces \ print i spaces i for \ loop from i to 0 inclusive [j]; \ in Forth, the innermost loop count is always i i 65 ...


0

Two tied solutions: Zsh, 71 bytes for ((;#1;)){s= for y ({A..Z})((#1==#y))&&s+=$y&&1=${1:1}||s+=\ <<<$s} Try it online! This uses straightforward comparison/ternary. Zsh, 71 bytes for ((;#1;)){s= for y ({A..Z})s+=${${(M)1[1]%$y}:-\ }&&1=${1#$y} <<<$s} Try it online! This is a bit more clever, appending to s ...


0

05AB1E, 21 19 17 bytes Çü@0šÅ¡vð₂×yAykǝ, Try it online! Ç push a list of ascii values of the input [84, 69, 83, 84] ü@ determine which letter is >= the next letter [1, 0, 0] 0š prepend a 0 (false) to that list [0, 1, 0, 0] Å¡ split input on true values [["T"], ["E", "S", "T"]] v ...


0

Whitespace, 163 bytes Try it online! (s = space, t = tab, l = linefeed) sssttstsl push 26 For x = 26 times lssl start: "" sls dup (Stack: [26, 26]) For y = x times lsssl spaces: ssstl push 1 tsst sub decrement space counter sls ...


2

Brachylog, 16 bytes ∧Ẓa₁ụ?b{~wṢ}ᵐ&ẉ⊥ Try it online! Prints with a single trailing newline.


1

Ruby -n, 62 bytes Scans the input string for sequences of increasing letters, and for each sequence, replace letters not in the sequence with spaces. g=*?A..?Z $_.scan(/#{g*??}?/){puts g.join.tr"^#$&"," "if$&[0]} Try it online!


1

Ruby, 58 bytes Not the best answer for Ruby, but still good enough to get second place out of three entries lol. I just wanted to try a regex-based solution. s=' '*26+[*?A..?[].reverse*'' puts s while s.sub!(/ \S/){} Try it online!


0

Python 2, 137 bytes s=input();n=len(s);m=2*n-2 for r in range(m+1):print''.join([s[[x,-8-x][(x<=m)==(m>2*r)]%n],' '][r!=(x+1-n)%m!=m-r]for x in range(4*n-3)) Try it online! A full program that takes a string as input as prints out the diamondized version.


1

C (clang), 374 bytes #define F(X,R)*d=*(d=o+x*(l*3+35-g)+x/2+X+R)-32?*d:R? i,z,x,l,w,r,g,m;f(n){int*d,o[i=z=(x=n*8+1)*(n*5+4)],*c=o;for(;l=i--;)*c++=i%x?32:10;for(;l++<n;)for(g=35;i=g-29;g-=3){for(r=m+=m=w=l*2;r--;F(~-w*x-w,r)g:g)F(w*~x,r)95:95,F(w*x-w,r)95:95;for(;w--;)for(r=4;r--;F(m-~w*~-x,-r)g:47)F(w-m-w*x,r)g:47,F(w-m-~w*x,r)g:92,F(m+~w-w*x,-r)g:92;}...


1

Add++, 58 bytes D,g,@,FL1+ D,k,@~,J L,R€gd"#"€*$dbM€_32C€*z£+bUBcB]bR€kbUn Try it online! How it works Our program consists of two helpers functions, g and k, and the main lambda function. g iterates over each integer, \$x\$, between \$1\$ and \$n\$ and returns \$d(x)\$ as defined in the challenge body, while k takes a list of characters and concatenates ...


0

SOGL V0.12, 971 bytes I simply copied the whole string to the compressor. I realized that the string wasn't compressed a lot. "ξ↔⁴‚σ╔↔æz½↓«ļ;¾⅝↑¶q$+ξρI#↔@≥Β⁄,;∆n2BΡ¾~«n▼og╤iCΘ⌠δZ░∑℮E3č─æ`⅓+↑žS]ķø 'αN²'Q-ω⅞±ž<e⁴Κ№6‼Dfθ∫⅞)i≠Ph╗8B‽`HΔ→┘↓d5∑∫h╤kΖΜY⅞|⌡Σχ¡GΥq«≡─ηe→°~⁹*░κRΝycč□±H⅔b¾℮╗↕Θ*ζΜ9⁵Dæqēυ¦Jn|¼▲ū-⁹¡╗;g²T℮F6u*ε¤3⅜v■└$}Q;▒MKηBqο⁰X╬/Lβ┌I╬č¤¾►'█p¹A0E∑QXγ9§...


1

Python 3, 140 bytes def f(a):l=len(a[0]);k=len(a)-l;b=[k*' '+i for i in-k*[l*' ']+a];b+=[*map(''.join,zip(*b))][::-1];return[i+j[::-1]for i,j in zip(b,b[::-1])] Try it online! Expects and returns a list of lines. Explanation def f(a): l=len(a[0]);k=len(a)-l; # Extract dimensions b=[k*' '+i for i in-k*[l*' ']+a]; # Make it ...


2

05AB1E, 6 bytes LN71SΛ Try it online! Λ use the canvas function with L a range list [1 .. input] as lengths for each path N a "0" as character to be printed (N is the index variable used by loops. If there was no loop yet, its default value is 0. By using N, I avoid an extra space between 0 and 71) ...


2

J, 78 75 bytes (26{.65|.a.)($~#)`(;/@])`(' '$~1+{:@])}i.@+:(,.~(|.,])@i.@-:@#)@#~1+i.@-,i. Try it online! -3 thanks to ngn


1

C (clang), 201 196 188 bytes x,y,i,v,m,n;f(s,z){char*a=s,o[i=(y=z*2-1)*(x=y+y)];for(v=x*2-2;i;n=m=1)o[--i]=i%x?32:10;for(i=x*--z;v--;i+=x*m+n)o[i]==32?o[i]=*a:0,a=*++a?a:s,n=i%x>x-3?-1:n,m=i/x?i/x<y-1?m:-1:1;puts(o);} Try it online! -13 @ceilingcat suggestions


9

05AB1E, 12 bytes gIR7._•Íη•Λ Try it online! Λ use the canvas function g with the length of input for each segment IR7._ the input reversed and rotated left by 7 characters (we will draw this backwards to have the right center character) •Íη• and the directions 1, 3, 3, 1, 7, 5, 5, 7 as a compressed number.


2

Red, 217 bytes func[a][n: max(l: length? a)length? a/1 p: copy""insert/dup p" "n loop n - l[insert a copy p]forall a[pad/left a/1 n]repeat y n[repeat x n[append a/:y a/(n - x + 1)/:y]]repeat y n[append a reverse copy a/(n - y + 1)]] Try it online! Returns a list of strings


3

JavaScript (ES6), 136 bytes f= (s,l=s.length-1,z=l*4,a=[...Array(l-~l)].map(_=>Array(z+1).fill` `),g=x=>x--?g(x,a[a[y=(x+l)%z]?y:z-y][x>z?z+z-x:x]=s[x%-~l]):a)=>g(z+z) <input oninput=o.textContent=f(this.value).map(c=&gt;c.join``).join`\n`><pre id=o> Returns a two-dimensional array. Works by drawing the string into the ...


1

Icon, 255 206 bytes procedure f(a) n:=*a;n<:=t:=*a[1] 1to n-*a&push(a,repl(" ",t))&\z (!a)[1:1]:=repl(" ",n-t)&\z i:=1to n&j:=1to n&a[i]||:=a[n-j+1,i]&\z i:=1to n&put(a,"")&j:=1to 2*n&a[n+i]||:=a[n-i+1,2*n-j+1]&\z return a end Try it online!


2

J, 79 77 75 bytes (|.@$~8*])`([:;/(1,~<:@])+/\@,(_2{&1 _1\#:33495)}.@#~])`(' '$~1+2 4*])}<:@# Try it online!


6

JavaScript (ES6),  157 155  154 bytes Returns a matrix of characters. s=>(y=n=s.length,y+=i=X=Y=-1,m=[...Array(y+n)].map(_=>r=Array(4*n-3).fill` `),g=x=>x?g(x-=r[m[y][++i==6*n-6||+x]=s[i%n],y-=m[y-Y]?Y:Y=-Y,x-X]?X:X=-X):m)`` Try it online! How? Given the length \$n\$ of the input string, we build a matrix of size \$w\times h\$, with: ...


10

Charcoal, 17 bytes GH<↗↘>↖↙LθθGH<Lθθ Try it online! Link is to verbose version of code. Explanation: GH Draw along a path. <↗↘>↖↙ Draw in the directions ↘↗↗↘↙↖↖↙ (the < and > are shorthands for those two pairs, but the other pairs don't have shorthands.) Lθ Each path segment has the same length, including the ends, of the length ...


3

Java (JDK), 180 178 bytes Not a single extra import: (y,m,n)->{for(m*=n;y.length()<m;y+=" ");var s="";for(;m-->0;y=s)for(var c:y.split(s=""))s=Math.random()<.5?s+c:c+s;s="#".repeat(n);return(s+y+s).replaceAll(".{"+n+"}","\n#$0#");} Try it online! It was quite the struggle to get this golfed down. In particular, the imports involved with ...


Top 50 recent answers are included