| Bytes | Lang | Time | Link |
|---|---|---|---|
| 099 | JavaScript Node.js | 240715T083445Z | Andrew B |
| 170 | Pure bash | 240707T155200Z | F. Hauri |
| 075 | GolfScript | 240702T072612Z | Twilight |
| 118 | Python 3.8 prerelease | 240705T124844Z | Jakque |
| 088 | Zsh | 240628T022041Z | GammaFun |
| 102 | JavaScript ES6 | 240625T130346Z | Arnauld |
| 097 | Perl 5 F | 240625T211527Z | Xcali |
| 066 | APL+WIN | 240625T155141Z | Graham |
| 150 | Python 3.8 prerelease | 240625T003655Z | squarero |
| 017 | Charcoal | 240625T000710Z | Neil |
| 078 | J | 240625T032739Z | Conor O& |
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
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 \ \\
GolfScript, 82 81 76 75 bytes
{49='/\ \ \ \/ \ \ '10/=}%5/zip.3<{' ':a+}%\3>{a\+}%+zip.,.a*:b@*b\+\6+/n*
LSB at the bottom left.
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)
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)
Zsh, 88 bytes
for b;((b))&&L=(\ $^L \\$l)&&l=' \'||{L=(' '$^L '/\'$l '\ \');l=' \/' }
print -rl $L $l
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
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=" \\"
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)
Perl 5 -F, 104 97 bytes
@;=((($b=$"x(length($;[0].=" \\".'/'x!$_)-3+$_))."/\\","$b\\".' \\'x!$_)[$_..1],@;)for@F;say for@
APL+WIN, 66 bytes
Prompts for bit vector. Index origin = 0
n←(⊂3 2⍴' \ \'),⊂3 3⍴'/\ \ \ \'⋄(-⌽⍳⍴s)⊖(i,i)⍴((i←⍴s)*2)↑n[s←~⎕]
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]])
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.
J, 78 bytes
' \/'{~[:+&(+|:&.|.)/1 2([*Q=/~([+|.@Q=:i.@#@])*_ 1{~]>.~2-[)"{0,&;(0 1;0){~|.
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