23
\$\begingroup\$

(easy mode of Seven Slash Display)

Given a positive integer (or alternatively, a string/list of bits of its binary representation) output it with this diagonal ASCII-art formation:

(output for 53 / 110101)

       \
     /\ \
     \ \
    \ \/
  /\ \
  \ \
 \ \/
\ \
 \

This output has the MSB at the bottom left. Alternatively, you may choose to output with the LSB at the bottom left.

The output may include any extra whitespace as long as that doesn’t impact the presentation of the image.

This is a competition, so the shortest code wins.

\$\endgroup\$

11 Answers 11

6
\$\begingroup\$

Charcoal, 23 17 bytes

FS¿Σι↗\«\↑/↗\»‖M↖

Try it online! Link is to verbose version of code. Takes input as a binary string. Explanation:

FS

Loop over each digit.

¿Σι

If it's a 1, then...

↗\

... output the bottom half of the 1, finishing with the cursor moved up and right.

«\↑/↗\»

Otherwise, draw the bottom half of the zero, finishing with the cursor moved up and right.

‖M↖

Reflect to complete the output.

Alternative solution, also 17 bytes:

FS«F¬Σι↑\¶/↗\»‖M↖

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

FS«

Loop over each digit.

F¬Σι

If it's a zero, then...

↑\¶/

... draw the \/ part.

↗\

Draw the bottom half of the 1 or complete the bottom half of the zero, finishing with the cursor moved up and right.

»‖M↖

Reflect to complete the output.

Edit: Saved 6 bytes by using ReflectMirror(:UpLeft); as suggested by @noodleman.

\$\endgroup\$
6
  • \$\begingroup\$ Nice solution! I initially posted this as a CMC in the TNB discord server, and somebody found a 20 byte Charcoal solution, and that takes the input as decimal, I wonder if you can find it? :) Hint: ReflectMirror(:UpLeft); \$\endgroup\$ Commented Jun 25 at 0:19
  • \$\begingroup\$ I found two solutions using ReflectMirror(:UpLeft);, but they weren't 20 bytes... \$\endgroup\$
    – Neil
    Commented Jun 25 at 7:19
  • \$\begingroup\$ Nice improvement! The person who had the original 20 bytes suggests using array input instead of string input to save another byte: Try it online! \$\endgroup\$ Commented Jun 25 at 10:47
  • \$\begingroup\$ Oh, you said input as decimal, well, in that case, it would cost two bytes to convert that to an array, but as you point out, the array then saves a byte, so it's still only 18 bytes, not 20: Try it online! \$\endgroup\$
    – Neil
    Commented Jun 25 at 12:43
  • \$\begingroup\$ I don’t understand your comment, I’m saying that this solution you wrote can be a byte shorter by taking the input as a list of binary digits rather than as a binary string. The other person’s original solution was 20 bytes by taking input as decimal or 18 bytes taking it as a binary string, so your solution is definitely shorter, they found the array-input improvement after looking at yours \$\endgroup\$ Commented Jun 25 at 15:46
4
\$\begingroup\$

Python 3.8 (pre-release),  263   243   229   226   225   187   168   151  150 bytes

-20 bytes by applying some small modifications.
-14 bytes by emanresu A.
-3 bytes by noodle person.
-75 bytes by ASCII-only.
-1 byte by noodle person.

…Okay, what—

def f(b,c=[]):
 for i in b:c+=[0,1,2][i::2]
 for i in range(l:=len(c),-1,-1):print(" "*i+"  /"[[0,*c][i]]+" \\"[i>0]+" \\"*(i<l)+"  /"[[*c,0,0][i+1]])

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ 229 \$\endgroup\$
    – emanresu A
    Commented Jun 25 at 6:01
  • \$\begingroup\$ Building on emanresu A's suggestion, you can alias range to r to save another 3 bytes. Try it online! \$\endgroup\$ Commented Jun 25 at 10:30
  • \$\begingroup\$ and another -1 \$\endgroup\$
    – ASCII-only
    Commented Jun 25 at 10:52
  • \$\begingroup\$ 187 \$\endgroup\$
    – ASCII-only
    Commented Jun 25 at 11:03
  • \$\begingroup\$ 168 \$\endgroup\$
    – ASCII-only
    Commented Jun 25 at 11:10
3
\$\begingroup\$

J, 78 bytes

' \/'{~[:+&(+|:&.|.)/1 2([*Q=/~([+|.@Q=:i.@#@])*_ 1{~]>.~2-[)"{0,&;(0 1;0){~|.

Try it online!

Accepts input as a list of binary digits. Here's the expanded version I started with, which shows some of the symmetry:

b =: 1 1 0 1 0 1
mask =: 0,;(0 1;0){~|.b

e =: [ + i.@-@#@]
mK =: 2 e mask
mT =: 1 e mask

K =: 2 * (mK *_ 1{~mask) =/ i.#mask
T =: 1 * (mT           ) =/ i.#mask

echo ' \/' {~ (+|:&.|.) K+T

Try it online!

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

Zsh, 88 bytes

for b;((b))&&L=(\ $^L \\$l)&&l=' \'||{L=('  '$^L '/\'$l '\ \');l=' \/' }
print -rl $L $l

Try it online!

Or add --rcexpandparam to remove the ^ for 86 bytes.

for bit;
    ((bit)) &&
        # prepend all but one of the previous lines with a space,
        # the last line prefixed with a '\'
        lines=(\ $^lines \\$last) &&
        # new last line is ' \'
        last=' \' ||
    {
        
        # prepend all but one of the previous lines with two spaces,
        # the last line prefixed with a '/\',
        # add '\ \' as the center of the zero
        lines=('  '$^lines '/\'$last '\ \')
        # new last line is ' \/'
        last=' \/'
    }
print -rl $lines $last
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 102 bytes

Expects an integer and outputs with the LSB at the bottom left.

f=(n,i,p=`
`,b=n-~n>>i/2)=>b>1?f(n,b/2%2-~i,p+" ")+p+"/ "[b&1|i&1]+s[+!!i]+s+"/  "[b&2|i&1]:p+=s=" \\"

Try it online!

Commented

f = (          // f is a recursive function taking:
  n,           //   n = input integer
  i,           //   i = counter (initially undefined)
  p = `\n`,    //   p = prefix string
  b = n - ~n   //   b = n * 2 + 1 right shifted by floor(i / 2)
      >> i / 2 //
) =>           //
b > 1 ?        // if b is greater than 1:
  f(           //   do a recursive call:
    n,         //     pass n unchanged
    b / 2 % 2  //     add 2 to i if the bit #1 of b is set
    - ~i,      //     or 1 otherwise (*)
               //     (because zeros are twice as large as ones)
    p + " "    //     append a space to the prefix string p
  ) +          //   end of recursive call
  p +          //   append the prefix string p
  "/ "[        //   append a space if:
    b & 1 |    //     the target digit is a one
    i & 1      //     or i is odd (grid misalignment)
  ] +          //   or a "/" otherwise (the top of a zero)
  s[+!!i] +    //   append "\" if i > 0, or a space otherwise
               //   (top segment of previous digit)
  s +          //   append " \" (bottom segment of new digit)
  "/  "[       //   append a space if:
    b & 2 |    //     the target digit is a one
    i & 1      //     or i is odd (grid misalignment)
  ]            //   or a "/" otherwise (the bottom of a zero)
:              // else (end of recursion):
  p +=         //   append p
    s = " \\"  //   followed by " \" (top segment of MSB)
\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 66 bytes

Prompts for bit vector. Index origin = 0

n←(⊂3 2⍴'  \  \'),⊂3 3⍴'/\ \ \ \'⋄(-⌽⍳⍴s)⊖(i,i)⍴((i←⍴s)*2)↑n[s←~⎕]

Try it online! Thanks to Dyalog Classic

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

Perl 5 -F, 104 97 bytes

@;=((($b=$"x(length($;[0].=" \\".'/'x!$_)-3+$_))."/\\","$b\\".' \\'x!$_)[$_..1],@;)for@F;say for@

Try it online!

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

Python 3.8 (pre-release), 120 118 bytes

def f(i):
 c=a,=["\n"]
 while i:c[0]+=" \\"+(x:=~i%2)*"/";c=[a+x*"/"+"\\",x*(a+"\\ \\")]+c;a+=-~x*" ";i//=2
 print(*c)

Try it online!

Output with a leading newline, some trailing whitespaces at the end of each line and with the least significant bit in the bottom left (LSB)

\$\endgroup\$
2
  • \$\begingroup\$ Nice solution! What does c=a,=["\n"] do? I've never seen that syntax for assignment in Python before. \$\endgroup\$ Commented Jul 6 at 17:23
  • 1
    \$\begingroup\$ @noodleperson It is a shorter version of c=["\n"];a="\n" \$\endgroup\$
    – Jakque
    Commented Jul 6 at 22:17
1
\$\begingroup\$

GolfScript, 82 81 76 75 bytes

{49='/\ \  \ \/ \ \ '10/=}%5/zip.3<{' ':a+}%\3>{a\+}%+zip.,.a*:b@*b\+\6+/n*

Try it online!

LSB at the bottom left.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Really nice, I was hoping for a GolfScript solution and this is a very cool way of doing it \$\endgroup\$ Commented Jul 2 at 10:53
1
\$\begingroup\$

Pure bash 170

Reading other answers here and re-reading request, there is a version without integer to binary conversion:

l=${1//1};t=${#1};printf -vk '%*s' $[t+${#l}-1];p=;for((c=t;c--;)){
((${1:c:1}))&&echo "$k\ $p"&&p=\\||{
k=${k:1};echo "$k/\ $p"$'\n'"$k\ \\";p=\\/;};k=${k:1};};echo \ \\

Pure bash 204

Full version expecting integer as argument.

for((a=$1;a;a>>=1)){ s=$[a&1]$s;};l=${s//1} t=${#s};printf -vk '%*s' $[t+${#l}-1]
p=;for((c=t;c--;)){ ((${s:c:1}))&&echo "$k\ $p"&&p=\\||{
k=${k:1};echo "$k/\ $p"$'\n'"$k\ \\";p=\\/;};k=${k:1};};echo \ \\
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js),  130  99 bytes

f=(n,p=`
`,q=`  `,k=` \\`)=>n?f(n>>1,p+(n&1?` `:`  `),n&1?k:`/\\`)+p+(n&1?q+k:` ${k+k+p+q+k}/`):p+q

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.