17
\$\begingroup\$

You have to write a program or a function in any language that outputs this pattern:

~|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||~
|~|||||||||||||||||||||||||||||||||||||||||||||||||||||||||~|
||~|||||||||||||||||||||||||||||||||||||||||||||||||||||||~||
|||~|||||||||||||||||||||||||||||||||||||||||||||||||||||~|||
||||~|||||||||||||||||||||||||||||||||||||||||||||||||||~||||
|||||~|||||||||||||||||||||||||||||||||||||||||||||||||~|||||
||||||~|||||||||||||||||||||||||||||||||||||||||||||||~||||||
|||||||~|||||||||||||||||||||||||||||||||||||||||||||~|||||||
||||||||~|||||||||||||||||||||||||||||||||||||||||||~||||||||
|||||||||~|||||||||||||||||||||||||||||||||||||||||~|||||||||
||||||||||~|||||||||||||||||||~|||||||||||||||||||~||||||||||
|||||||||||~|||||||||||||||||~|~|||||||||||||||||~|||||||||||
||||||||||||~|||||||||||||||~|||~|||||||||||||||~||||||||||||
|||||||||||||~|||||||||||||~|||||~|||||||||||||~|||||||||||||
||||||||||||||~|||||||||||~|||||||~|||||||||||~||||||||||||||
|||||||||||||||~|||||||||~|||||||||~|||||||||~|||||||||||||||
||||||||||||||||~|||||||~|||||||||||~|||||||~||||||||||||||||
|||||||||||||||||~|||||~|||||||||||||~|||||~|||||||||||||||||
||||||||||||||||||~|||~|||||||||||||||~|||~||||||||||||||||||
|||||||||||||||||||~|~|||||||||||||||||~|~|||||||||||||||||||

The output is composed of 20 lines of 61 characters each.

Rules

  • Standard loopholes are forbidden
  • There may be a single trailing newline at the end of the output
  • There may not be any trailing whitespace on any line of the output

Without a trailing newline, the md5 checksum of the output is fde4e3b4606bf9f8c314131c93988e96.

With a trailing newline, the md5 checksum of the output is 1f0b43db4fec6594be202c8339024cb7.

This is , so the shortest code in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ I have edited the challenge to make it slightly clearer, feel free to rollback the edit. \$\endgroup\$ – Kritixi Lithos Jul 11 '17 at 12:29
  • 1
    \$\begingroup\$ I've drawn a curious pattern ... rextester.com/WXZV81312 \$\endgroup\$ – sergiol Jul 11 '17 at 21:36

26 Answers 26

13
\$\begingroup\$

C (gcc), 97 82 81 80 bytes

Golfed 15 bytes after learning that abs is a builtin in C, an additional byte thanks to Rogem for pointing out that the declarations of my variables can be moved to the function, and another byte thanks to ceilingcat for suggesting x=31;--x+31 instead of x=-31;++x<31.

f(x,y){for(y=21;--y;puts(""))for(x=31;--x+31;)printf(abs(10-abs(x))-y?"|":"~");}

Try it online!

This outputs with a trailing newline. The function f does the outputting.

Explanation

The output can be stated as a graph.

~|||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||~
|~||||||||||||||||||||||||||||+||||||||||||||||||||||||||||~|
||~|||||||||||||||||||||||||||+|||||||||||||||||||||||||||~||
|||~||||||||||||||||||||||||||+||||||||||||||||||||||||||~|||
||||~|||||||||||||||||||||||||+|||||||||||||||||||||||||~||||
|||||~||||||||||||||||||||||||+||||||||||||||||||||||||~|||||
||||||~|||||||||||||||||||||||+|||||||||||||||||||||||~||||||
|||||||~||||||||||||||||||||||+||||||||||||||||||||||~|||||||
||||||||~|||||||||||||||||||||+|||||||||||||||||||||~||||||||
|||||||||~||||||||||||||||||||+||||||||||||||||||||~|||||||||
||||||||||~|||||||||||||||||||+|||||||||||||||||||~||||||||||
|||||||||||~|||||||||||||||||~+~|||||||||||||||||~|||||||||||
||||||||||||~|||||||||||||||~|+|~|||||||||||||||~||||||||||||
|||||||||||||~|||||||||||||~||+||~|||||||||||||~|||||||||||||
||||||||||||||~|||||||||||~|||+|||~|||||||||||~||||||||||||||
|||||||||||||||~|||||||||~||||+||||~|||||||||~|||||||||||||||
||||||||||||||||~|||||||~|||||+|||||~|||||||~||||||||||||||||
|||||||||||||||||~|||||~||||||+||||||~|||||~|||||||||||||||||
||||||||||||||||||~|||~|||||||+|||||||~|||~||||||||||||||||||
|||||||||||||||||||~|~|||||||||||||||||~|~|||||||||||||||||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

(The +s are only shown for explanation purposes and represent the axes.)

The equation of this graph is \$y=\text{abs}(10-\text{abs}(x))\$ as can be seen here in this link to a Desmos graph.

abs(10 - abs(x))
abs(           )            Reflect whatever is beneath the x-axis to above the x-axis
    10 - abs(x)             This forms the central triangle-like structure

In function f, we have two for-loops that iterate through every coordinate in this graph. y goes from 20 to 1 and x goes from -30 to 30.

For every x, we check if abs(10-abs(x)) equals y by doing abs(10-abs(x))-y in a ternary. If they are equal, this yields 0, a falsey value in C, otherwise it will evaluate to some positive value. Then in the ternary abs(10-abs(x))-y?"|":"~", we printf accordingly.

And after each line, we output a newline using puts(""), and that is how the function outputs with a trailing newline.

\$\endgroup\$
  • 1
    \$\begingroup\$ Save one byte with f(x,y) instead of x,y;f() \$\endgroup\$ – user77406 Dec 17 '18 at 16:12
  • \$\begingroup\$ @ceilingcat Thanks for the golf \$\endgroup\$ – Kritixi Lithos Oct 25 at 18:52
11
\$\begingroup\$

Charcoal, 16 15 bytes

-1 byte thanks to notjagan

↘×~²⁰↗↗×~χ‖OUB|

Try it online!

\$\endgroup\$
  • \$\begingroup\$ -1 byte by removing the and moving implicitly. \$\endgroup\$ – notjagan Jul 11 '17 at 15:34
4
\$\begingroup\$

R, 70 67 bytes

3 bytes thanks to Giuseppe.

write(c("|","~")[outer(abs(10-abs(-30:30)),20:1,"==")+1],"",61,,"")

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 67 bytes \$\endgroup\$ – Giuseppe Dec 19 '17 at 20:46
  • \$\begingroup\$ Change the first "" to a 1 for -1. \$\endgroup\$ – J.Doe Dec 17 '18 at 18:17
3
\$\begingroup\$

Jelly, 18 16 bytes

⁵×3ŒRAạ=þḤṚị⁾~|Y

Try it online!

⁵×3ŒRAạ=þḤṚị⁾~|Y  Main link. No arguments.

⁵                 Set the argument and the return value to 10.
 ×3               Multiply by 3 to yield 30.
   ŒR             Balanced range; yield [-30, -29, ..., 29, 30].
     A            Take absolute values.
      ạ           Take absolute differences with 10.
         Ḥ        Unhalve; yield 20.
       =þ         Table equals; compare each result with each k in [1, ..., 20].
          Ṛ       Reverse the resulting 2D array.
           ị⁾~|   Index into "~|", yielding '~' for 1 and '|' for 0.
               Y  Separate by linefeeds.
\$\endgroup\$
3
\$\begingroup\$

Python 2.7, 163 138 135 133 113 91 bytes

l,t=S='|~'
for s in range(20):a=[l]*61;a[s]=a[60-s]=t;a[40-s]=a[20+s]=S[s>9];print`a`[2::5]

Try it online!

Edit 1: -25 bytes: changed the algorithm after I felt a bit ambitious. :P

Edit 2: -3 bytes: courtesy Felipe Nardi Batista

Edit 3: -2 bytes: courtesy shooqie

Edit 4: -20 bytes: courtesy notjagan

Edit 5: -22 bytes: courtesy Leaky Nun

\$\endgroup\$
  • \$\begingroup\$ Completely missed that! Thanks @FelipeNardiBatista. It doesn't matter anymore though :P changed the algorithm completely. \$\endgroup\$ – Koishore Roy Jul 11 '17 at 14:08
  • 1
    \$\begingroup\$ 135 bytes \$\endgroup\$ – Felipe Nardi Batista Jul 11 '17 at 14:10
  • 1
    \$\begingroup\$ stacking attributions like a,b,c='1','2','3' is the same as a='1';b='2';c='3' and the same as placing each one in it's own line, but you can gain bytes by unpacking strings like a,b,c='123' \$\endgroup\$ – Felipe Nardi Batista Jul 11 '17 at 14:12
  • 1
    \$\begingroup\$ Isn't n<m shorter than n-m<0? \$\endgroup\$ – shooqie Jul 11 '17 at 14:33
  • 1
    \$\begingroup\$ Got it down to 113 bytes. \$\endgroup\$ – notjagan Jul 11 '17 at 15:28
3
\$\begingroup\$

///, 231 bytes

/3/|~//2/\/\///1/!!20/|
2-/&#2,/|#2'/"""2&/||2%/1|2#/&~2"/1!2!/&&|/~'%,
3'1#0#'1~&
,'!,&0-'!3&&
!~'-!
!3'#!0!#'~!&
!,""%#!&0!-""%~!&&
1~"-"-1
%~"#3"#%
1#"~,"~%0%#%#!~%#%&
%,%~!#%~%&0"~!-!-!-"
"3!#%~!#"0"#!~%#!~"&
",,"~,"&0"-3"#3"&&

Try it online! Or, view it interactively here!

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

WendyScript, 65 bytes (exclude newline)

<<a=>(x)?x<0/>-x:/>x
#y:20->0{#x:-30->31?a(10-a(x))==y@"~":@"|"""}

Try it online!

Follows the same principle as the C answer given above. The first line is the abs function, second line runs two for loops and outputs ~ or | based on the graph. The last "" is used to output a newline after each loop on y.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why are you excluding the newline? \$\endgroup\$ – Conor O'Brien Jul 13 '17 at 18:49
  • \$\begingroup\$ The newline isn't required, the newline can be removed and the script still runs fine. It's just there so it's easier to see the separation of the abs function vs the main function. \$\endgroup\$ – Felix Guo Jul 13 '17 at 18:59
2
\$\begingroup\$

Vim, 59 Bytes

2i~^[59i|^[qqYpi|^[f~l2xA|^[q18@q11G31|qqr~jlq9@qF~2lqqr~klq8@q

Where ^[ is the <ESC> key

\$\endgroup\$
  • \$\begingroup\$ :11<CR> can be 11G \$\endgroup\$ – nmjcman101 Jul 11 '17 at 15:41
  • \$\begingroup\$ Cheers. I didn't know about that one \$\endgroup\$ – bioweasel Jul 11 '17 at 16:22
2
\$\begingroup\$

Japt, 32 bytes

20ç|
AÆhX'~
VméA
VpWUVmw)c ê z ·

Try it online! Be sure to expand the output box.

Explanation

20ç|

Set U to | repeated 20 times.

AÆhX'~

Set V to the range [0,9] () mapped by:
U (implicit) with the character at index X (current value) set to (h) ~.

VméA

Set W to V with each line rotated 10 (A) chars right.

VpWUVmw

Create array: V, W, U, and V with each line reversed (w). This is now the left half of the shape, rotated 90° left.

c ê z ·

Flatten the array (c), palendromize it (ê), rotate 90° right (z), and join with newlines (·).

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

Paintbrush, 36 bytes

non-competing

b|20{s~>v}10{>^s~}9{>vs~}>v20{>^s~}▁

Explanation

b|20{s~>v}10{>^s~}9{>vs~}>v20{>^s~}▁  Program
b|                                    Sets the background character to `|`
  20{    }                            Executes function 20 times
     s~                               Sets the current character to `~`
       >v                             Moves one space right and one space down
          10{    }                    Executes function 10 times
             >^                       Moves one space right and one space up
               s~                     Sets the current character to `~`
                  9{    }             Executes function 9 times
                    >v                Moves one space right and one space down
                      s~              Sets the current character to `~`
                         >v           Moves one space right and one space down
                           20{    }   Executes function 20 times
                              >^      Moves one space right and one space up
                                s~    Sets the current character to `~`
                                   ▁  Cuts off the last line (because it pads an extra line when the pointer moves near the edge)

This reminds me, I need to add a mirror operation.

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

Octave, 157 57 54 bytes

Golfed it further down, thanks to the other answers and the comments.

a=zeros(20,61);for i=-30:30;a(21-abs(10-abs(i)),i+31)=2;end
a=char(a+124)

I just approached it like the other answer with the abs(10-abs(x)) function and then used the right ASCII characters to print out the image.

\$\endgroup\$
  • 1
    \$\begingroup\$ Here we require answers to be well-golfed in a code-golf challenge. At least you should attempt to golf it by for example removing unnecessary whitespace etc. \$\endgroup\$ – Erik the Outgolfer Jul 12 '17 at 13:05
  • 1
    \$\begingroup\$ Also, the language is called simply "Octave". As for golfing, apart from removing all whitespace (newlines too), make sure you combine statements, like the two final lines. \$\endgroup\$ – Sanchises Jul 12 '17 at 14:56
  • \$\begingroup\$ If anyone knows how I can get rid of the newline after endfor, that would be a great help. \$\endgroup\$ – Michthan Jul 13 '17 at 6:09
1
\$\begingroup\$

Pyth, 22 bytes

j_t.tm+*\|aTa30d\~61\|

Try it online!

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

V, 30 bytes

i~³°|19ñÙé|$xñ22|òr~klòÎæ$0Px

Try it online!

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

Jelly, 18 bytes

61Rạ31ạ⁵Ṭ€z0Ṛị⁾~|Y

Try it online!

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

Bubblegum, 90 bytes

00000000: 9dcb a10d 0040 08c5 50cf cc4d 673f 85ab  .....@..P..Mg?..
00000010: b880 22fd 7972 3f07 ef98 e1cc 85e1 ca05  ..".yr?.........
00000020: 8623 97d5 78c2 abf1 8457 e305 b31a 0f78  .#..x....W.....x
00000030: f507 0fcc 54fc 6ed3 794b b6d2 c1ed 163a  ....T.n.yK.....:
00000040: b8dd 42c7 68b7 d031 f757 3ab8 dd42 07b7  ..B.h..1.W:..B..
00000050: 5be8 e076 0b1d dcaf 060f                 [..v......

Try it online!

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

MathGolf, 22 bytes

I{S╒xñ♂-±Iï--mÆ┬û|~§yp

Try it online!

Explanation

It's probably possible to golf away 2-3 bytes from this, I'll see what I can do.

I                        push 20
 {                       start block or arbitrary length
  S                      push 30
   ╒                     range(1,n+1)
    x                    reverse int/array/string
     ñ                   pop(a), push palindromize(a) string/list/number
      ♂                  push 10
       -                 pop a, b : push(a-b)
        ±                absolute value
         I               push 20
          ï              index of current loop, or length of last loop
           -             pop a, b : push(a-b)
            -            pop a, b : push(a-b)
             m           explicit map
              Æ          start block of length 5
               ┬         check if equal to 0
                û|~      string "|~"
                   §     get from array/string
                    y    join array without separator to string or number
                     p   print with newline
\$\endgroup\$
  • \$\begingroup\$ As much as the auto generated explanations are convenient, they usually miss the point of why the program is doing that... \$\endgroup\$ – Jo King Dec 17 '18 at 22:45
  • \$\begingroup\$ @JoKing this is pretty much a port of the Jelly answer. I found a 21-byter after this using pretty much the same logic, I'll try to write a better explanation for that. \$\endgroup\$ – maxb Dec 18 '18 at 6:28
1
\$\begingroup\$

C (gcc), 75 bytes

f(x){for(x=1240;--x;)putchar(x%62?x/62+1-abs(10-abs(x%62-31))?124:126:10);}

Try it online!

Totally changed from Cows quack's answer

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

Positron, 165 bytes

i=0;while(i<20)do{k='|'*(59-2*i);if(i==10)then{j='|'*19;k=j+'~'+j;};if(i>10)then{q=39-2*i;j='|'*q;q=2*i-21;k=j+'~'+'|'*q+'~'+j;}print@('|'*i+'~'+k+'~'+'|'*i);i=i+1;}

Try it online!

I think Positron has too many bugs in it. I should get it updated to TIO because then ++ will actually work.

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

Mathematica, 78 75 bytes

Print[""<>Riffle[Array[If[#+Abs[10-Abs[31-#2]]==21,"~","|"]&,{20,61}],"\n"]]

except the \n is replaced by an actual newline. Try it online! (There are extra spaces at the starts of the lines in Mathics for some reason, but it works fine in Mathematica.)

I came up with a submission of my own, but then Kritixi Lithos added an explanation of theirs and it was quite similar to mine but using a slightly cleverer formula, so now this is just a port of that answer. (Go and read that one and upvote it!)

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

Bubblegum, 93 bytes

00000000: 9dcb 390e 4301 10c2 d09e 335b 9c3d 5d56  ..9.C.....3[.=]V
00000010: e72f 4c35 327a 65bf 86ee 9830 f342 5879  ./L52ze....0.BXy
00000020: 8130 f202 848d 9797 a613 262c bc7c 6a3a  .0........&,.|j:
00000030: 60c2 552e 9858 bcdc a2f9 55ac 9916 5e6f  `.U..X....U...^o
00000040: a285 d79b 6819 eb4d b4cc fe99 165e 6fa2  ....h..M.....^o.
00000050: 85d7 9b68 e1d5 26da 782f 3578 00         ...h..&.x/5x.

Try it online!

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

JavaScript (ES6), 87 bytes

for(a=Math.abs,y=20;y--;console.log(s))for(s='',x=-30;x<31;x++)s+=a(10-a(x))+~y?'|':'~'

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

Lua, 193 bytes

m={}function g(x,y)if(x<62)then
m[x+y*61]="~"if(x==31or x==21or x==41)then
b=not b
end
g(x+1,y+((b and-1)or 1))end
end
g(1,0)for y=0,19 do
s=""for x=1,61 do
s=s..(m[x+y*61]or"|")end
print(s)end

Note that Lua cannot print something out without creating a new line. For this reason, I have to break one of the rules.

Minimally minified to a large extent:

map={}
o=20
p=61
--b is true means go up
function bounce(x,y,c)
    if (x~=p)then
        map[x+y*p]=c 
        if(x==31 or x==21 or x==41)then 
            b=not b 

        end
        bounce(x+1,y+((b and -1) or 1),c)
    end
end
bounce(1,0,"~")
--map[2+60] = "h"
for y=0,o-1 do
    str = ""
    for x=1,p do
        str = str..(map[x+y*p] or "|")
    end
    print(str)
end

Some changes where made during minification, all of which makes the program less extendable but smaller.

Try it: https://tio.run/##PY7LCoMwEEX3@YqQVVJTcWwRCp0vKV1oNa1QJ0UjGPr49TRq6eoO91wOcx/LEDp8vs1IF9da4lc5aa9aI6djkSt3a4h1pynxmwLOKD5iJog7sD2Pmf9yD@u0QrKOV6yhmkVTAtonUla8pHoLKm5BqZmtHHSmTCw9ZhoOvLZsQCHMogRdwNoMaSr/L9hevMSiePQtOTnMdwhf

I'm not sure if anyone has done this before, but I tried minifying by loading the program as a string and using gsub(search/replace). Unfortainitly, it made the program bigger. However, if this program was big enough, it would yield in less bytes.

g=string.gsub
loadstring(g(g(g('m={}function g(x,y)if(x<62vm[x+y*61]="~"if(x==31zx==21zx==41vb=not beg(x+1,y+((b and-1)z1))eeg(1,0)fzy=0,19 do s=""fzx=1,61 do s=s..(m[x+y*61]z"|")eprint(s)e','e',' end '),'v',')then '),'z','or '))()

Due to its relative closeness with the real result(240 bytes, only 41 more), I figured I'd post it. If this program where 350+ bytes, there would have likely been a reduction.

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

Java 8, 113 bytes

v->{String r="";for(int i=-1,j;++i<20;r+="\n")for(j=61;j-->0;)r+=j==i|j+i==60|i>9&(j-i==20|j+i==40)?"~":"|";return r;}

I have the feeling the checks (j==i|j+i==60|i>9&(j-i==20|j+i==40) can definitely be golfed by somehow combining multiple checks into one.

Explanation:

Try it here.

v->{                    // Method with empty unused parameters and String return-type
  String r="";          //  Result-String
  for(int i=-1,j;       //  Index integers
      ++i<20;           //  Loop (1) from 0 to 20 (exclusive)
      r+="\n")          //    After every iteration: append a new-line to the result-String
    for(j=61;           //   Reset `j` to 61
        j-->0;)         //   Inner loop (2) from 60 down to 0 (inclusive)
      r+=               //    Append the result-String with:
         j==i           //     If `j` and `i` are equal (top-right /),
         |j+i==60       //     or `j` + `i` is 60 (top-left \),
         |i>9           //     or we're at the bottom halve
          &(j-i==20     //      and `j` - `i` is 20 (bottom left \),
            |j+i==40)?  //      or `j` + `i` is 40 (bottom right /)
          "~"           //       Append a literal "~" to the result-String
         :              //     Else:
          "|";          //      Append a literal "|" to the result-String
                        //   End of inner loop (2) (implicit / single-line body)
                        //  End of loop (1) (implicit / single-line body)
  return r;             //  Return the result-String
}                       // End of method
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 79 bytes

push@a,[('|')x61]for 1..21;map$a[20-abs 10-abs][$_+30]='~',-30..30;say@$_ for@a

Try it online!

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

Tcl, 104 bytes

time {incr j
set i 0
time {puts -nonewline [expr 21-abs(abs([incr i]-31)-10)-$j?"|":"~"]} 61
puts ""} 20

Try it online!


Tcl, 105 bytes

time {incr j
set i 0
time {puts -nonewline [expr $j==21-abs(abs([incr i]-31)-10)?"~":"|"]} 61
puts ""} 20

Try it online!


Tcl, 109 bytes

time {incr j;set i 0;time {append s [expr $j==21-abs(abs([incr i]-31)-10)?"~":"|"]} 61;set s $s\n} 20
puts $s

Try it online!

Tcl, 143 133 123 110

Still much ungolfed, but I will evolve it after:

time {incr j;set i 0;time {incr i;append s [expr $j==21-abs(abs($i-31)-10)?"~":"|"]} 61;set s $s\n} 20
puts $s

demo

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

05AB1E, 20 19 bytes

20F„|~20Ýû31∍ûNQèJ,

Try it online.

Explanation:

20F               # Loop 20 times:
   „|~            #  Push the string "|~"
   20Ý            #  List of range [0,20]
      û           #  Palindromize [0..20..0]
       31∍        #  Shorten to length 31 [0..20..10]
          û       #  Palindromize again [0..20..10..20..0]
           NQ     #  Check if the loop index is equal to it
             è    #  And index it into the string
              J   #  Then join the list of characters together
               ,  #  And print with trailing newline

20Ýû31∍û generates the list:

[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,11,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0]
\$\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.