12
\$\begingroup\$

Below is a (schematic) Digital timing diagram, for the XNOR logic gate.

    ┌─┐ ┌─┐ ┌─────┐ ┌─┐ ┌─┐ ┌───┐       
A ──┘ └─┘ └─┘     └─┘ └─┘ └─┘   └──
  ┌───┐ ┌───┐ ┌─┐ ┌─────┐   ┌─┐ ┌─┐ 
B ┘   └─┘   └─┘ └─┘     └───┘ └─┘ └
    ┌─────┐   ┌─┐   ┌─┐   ┌───┐   
X ──┘     └───┘ └───┘ └───┘   └────

Your goal is to reproduce it exactly as depicted.

Rules:

  • You can either print it or return a multiline string;

  • Arbitrary number of traling and/or leading newlines are allowed;

  • Trailing (but not leading !) whitespace is allowed;

  • If you can not use the extended ASCII box-drawing characters, you may substitute them for the unicode equivalents (at no byte penalty).

This is so the shortest answer in bytes wins.

Binary Representation

For your convenience, binary representation of the diagram above is as follows:

INP A=0101011101010110
INP B=1101101011100101
  ___
X=A⊕B=0111001001001100

Sample Output

enter image description here

Sidenote

While working on this question, I've implemented two different bash solutions for it, one is 122 characters/bytes long (as depicted above), and another one is exactly 100 bytes long.

I do not have plans to post them (as I don't normally post answers to my own questions), so that is just for reference.

I also believe that at least some sub-100 byte solutions are possible.

\$\endgroup\$
  • 2
    \$\begingroup\$ That's an XNOR gate, isn't it... \$\endgroup\$ – clismique Jan 6 '17 at 13:33
  • \$\begingroup\$ @Qwerp-Derp Yep, maybe it is :) \$\endgroup\$ – zeppelin Jan 6 '17 at 13:52
  • 2
    \$\begingroup\$ Instead of suggesting (0==0)==0 shan't either B or X start and end at 1? \$\endgroup\$ – Roman Czyborra Jan 6 '17 at 14:11
  • \$\begingroup\$ @Roman Czyborra Not sure I understand the idea, could you elaborate on this a bit ? \$\endgroup\$ – zeppelin Jan 9 '17 at 10:14
  • 1
    \$\begingroup\$ @zeppelin Look right at the beginning of the pattern - the first couple pixels. A, B and X are all low there. The same is true at the end. Why is this the case? (I believe this is Roman's question). \$\endgroup\$ – isaacg Jan 9 '17 at 11:19
5
\$\begingroup\$

05AB1E, 101 bytes + 5 UTF-8 bytes = 116 Total Bytes = 106 Bytes

(LEGACY 05AB1E VERSION, NO LONGER ON TIO)

•=(Ín§Àoà`œ¯_eè8y1ÜŸ,Ú®:¹$:,õKA–x[Âì0ãXÔfz}y×ì¹Ï½uEÜ5äÀTë@ºQÈ™ñó:ò…Eä•6B"102345"" ┌─┐└┘"‡6ävyN" A B X"èì}»

Try it online!

The compression:

•=(Ín§Àoà`œ¯_eè8y1ÜŸ,Ú®:¹$:,õKA–x[Âì0ãXÔfz}y×ì¹Ï½uEÜ5äÀTë@ºQÈ™ñó:ò…Eä• 
# Pattern, converted to base-6 in base-6=214.

111023102310222223102310231022231112251425142511111425142514251114221022231022231023102222231110231023151114251114251425111114222514251411102222231110231110231110222311111225111114222514222514222511142222
# Actual base-6 pattern.

1110231023102222231023102310222311
1225142514251111142514251425111422
1022231022231023102222231110231023
1511142511142514251111142225142514
1110222223111023111023111022231111
1225111114222514222514222511142222
#Pattern split into chunks of 34.

   ┌─┐ ┌─┐ ┌─────┐ ┌─┐ ┌─┐ ┌───┐  
 ──┘ └─┘ └─┘     └─┘ └─┘ └─┘   └──
 ┌───┐ ┌───┐ ┌─┐ ┌─────┐   ┌─┐ ┌─┐
 ┘   └─┘   └─┘ └─┘     └───┘ └─┘ └
   ┌─────┐   ┌─┐   ┌─┐   ┌───┐    
 ──┘     └───┘ └───┘ └───┘   └────
# Pattern after replacing 0,1,2,3,4,5 with appropriate blocks.

The conversion:

6B                                   # Convert back to base-6.
  "102345"" ┌─┐└┘"‡                  # Replace numbers with appropriate counterparts.
                   6ä                # Split into 6 equal parts (the rows).
                     vy           }  # For each row (chunk).
                       N" A B X"èì   # Push label at index [i], prepend to line.
                                   » # Print all separated by newlines.

Using the CP-1252 encoding.

\$\endgroup\$
  • \$\begingroup\$ You are allowed to count each unicode box-drawing character as just 1 byte, for the purposes of this challenge (see "Rules"), so your answer is 106 bytes long according to this. The rationale is that these unicode characters can be replaced with 1 byte characters e.g. in CP437 or IBM850, but these can be more tricky to display. \$\endgroup\$ – zeppelin Jan 12 '17 at 21:28
  • \$\begingroup\$ I agree with zeppelin. Given the concession given in the rules, this would therefore be 106 bytes. \$\endgroup\$ – Level River St Jan 12 '17 at 21:54
  • \$\begingroup\$ Alternative method that only works in the new version of 05AB1E using the builtin Åв (I've also updated the ASCII-art generator tip with this): 106 bytes \$\endgroup\$ – Kevin Cruijssen Aug 12 at 9:18
4
\$\begingroup\$

Bubblegum, 76 bytes

00000000: 92d6 3000 5431 1505 1403 50e8 4e0a aafc  ..0.T1....P.N...
00000010: 9f62 15e6 a3ff 61fa dc05 e06d 8b66 cbc7  .b....a....m.f..
00000020: e6b6 cff8 519a b85a 3eb6 b67d 95c0 0feb  ....Q..Z>..}....
00000030: 35b5 521d 7f7e 68af a916 fa20 d999 564d  5.R..~h.... ..VM
00000040: 1f03 d559 59ed 265c f243 42be            ...YY.&\.CB.

Try it online!

Uses box-drawing characters from the VT100 alternate character set, which TIO can’t demonstrate. Run in a UNIX terminal for best results. My terminal converts the ACS to UTF-8 on copy-and-paste, so you can see the effect here.

anders@change-mode:/tmp$ bubblegum xnor.zlib
    ┌─┐ ┌─┐ ┌─────┐ ┌─┐ ┌─┐ ┌───┐
A ──┘ └─┘ └─┘     └─┘ └─┘ └─┘   └──
  ┌───┐ ┌───┐ ┌─┐ ┌─────┐   ┌─┐ ┌─┐
B ┘   └─┘   └─┘ └─┘     └───┘ └─┘ └
    ┌─────┐   ┌─┐   ┌─┐   ┌───┐
X ──┘     └───┘ └───┘ └───┘   └────
▒┼␍␊⎼⎽@␌␤▒┼±␊-└⎺␍␊:/├└⎻$ 

Well, the challenge didn’t say we need to take the terminal back out of ACS mode before returning to the shell. Good luck with that.

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

Ruby, 113 bytes

counting printed symbols as one byte as authorised by the challenge (I was surprised to discover they are actually 3 bytes.)

6.times{|i|s=' A B X'[i]
'D]zunIWkF]nIRukFH'.bytes{|b|s+='   ┌─┐───┘ └'[(b*2>>i/2*2&6)-i%2*6,2]}
s[1]=' '
puts s}

6 lines of output lends itself to an encoding of 6 bits per each character of the magic string. But the magic string characters actually encode for each transition thus:

least significant bit 0 New value for A  
                      1 Current value for A
                      2 New value for B
                      3 Current value for B
                      4 New value for X
                      5 Current value for X
most significant bit  6 Always 1 (to remain in printable range)

This is decoded to find the 2 characters that must be printed for each transition (the first of which is either space or a horizontal line.) The 8-character strings for the upper and lower rows overlap: The last two characters for the upper row 11 are two horizontal lines, which matches with what is needed for the first two characters of the lower row 00. The 8 characters for the lower row wrap around: they are last 6 and first 2 characters of the symbol string.

Ungolfed code

6.times{|i|s=' A B X'[i]               #iterate through 6 lines of output. Set s to the 1st character.
  'D]zunIWkF]nIRukFH'.bytes{|b|        #for each byte in the magic string
     s+='   ┌─┐───┘ └'[(b*2>>i/2*2&6)- #add 2 bytes to s, index 0,2,4, or 6 of the symbol string depending on relevant 2 bits of the magic string.
     i%2*6,2]                          #if the second (odd) row of a particular graph, modify index to -6,-4,-2, or 0 
  }                                    #(ruby indices wrap around. mystring[-1] is the last character of the string.)
  s[1]=' '                             #replace intitial ─ of the curve with space to be consistent with question
  puts s                               #output line
}
\$\endgroup\$
  • \$\begingroup\$ This is actually 129 bytes, 113 characters. \$\endgroup\$ – Magic Octopus Urn Jan 12 '17 at 20:32
  • 1
    \$\begingroup\$ @carusocomputing I see you have made the same comment on most of the answers here. Please refer to the first line of my anser and to rule 4 of the OP: If you can not use the extended ASCII box-drawing characters, you may substitute them for the unicode equivalents (at no byte penalty) We have all scored our answers according to the rules. \$\endgroup\$ – Level River St Jan 12 '17 at 21:51
2
\$\begingroup\$

PowerShell, 255 characters, 265 bytes (UTF-8)

$a='    012 012 0111112 012 012 01112
A 113 413 413     413 413 413   411
  01112 01112 012 0111112   012 012
B 3   413   413 413     41113 413 4
    0111112   012   012   01112
X 113     41113 41113 41113   41111'
0..4|%{$a=$a-replace$_,('┌─┐┘└'[$_])};$a

This works on my computer, but doesn't seem to parse the bytes correctly on TIO...

screenshot

This sets $a to be a multi-line string filled with numbers and spaces, then loops 0..4|%{...}. Each iteration, we -replace the appropriate digit $_ with the appropriate character '┌─┐┘└'[$_] and store it back into $a. Then, we just leave $a on the pipeline and output is implicit.

\$\endgroup\$
  • \$\begingroup\$ You are allowed to count the unicode characters as single bytes, according to the challenge \$\endgroup\$ – FlipTack Jan 8 '17 at 10:10
2
\$\begingroup\$

JavaScript (ES6), 163 158 154 bytes

NB: counting UTF-8 characters as bytes, as authorized by the challenge.

_=>[..." A B X"].map((c,i)=>c+" "+[...Array(33)].map((_,j)=>j%2?" ─"[p^i&1]:" ┐┌─└┘ "[p+(p=[27370,42843,12878][i>>1]>>j/2&1)*2+i%2*3]).join``,p=0).join`
`

Demo

let f =

_=>[..." A B X"].map((c,i)=>c+" "+[...Array(33)].map((_,j)=>j%2?" ─"[p^i&1]:" ┐┌─└┘ "[p+(p=[27370,42843,12878][i>>1]>>j/2&1)*2+i%2*3]).join``,p=0).join`
`

console.log(f())

Saved 4 bytes thanks to Neil

\$\endgroup\$
  • \$\begingroup\$ j%2?" ─"[p^i&1]:" ┐┌─└┘ "[p+(p=[27370,42843,12878][i>>1]>>j/2&1)*2+i%2*3] saves 3 bytes. \$\endgroup\$ – Neil Jan 6 '17 at 22:01
  • \$\begingroup\$ Oh, and change 17 to 33 too. \$\endgroup\$ – Neil Jan 6 '17 at 22:02
  • \$\begingroup\$ Once again, 152 characters, 164 bytes. \$\endgroup\$ – Magic Octopus Urn Jan 12 '17 at 20:33
2
\$\begingroup\$

C, 213 205 bytes

For a change, the C program size, on this challenge, isn't completely ridiculous compared to other languages.

#define X(a) u[i]=C[a],l[i++]=C[(a)+4]
p(n,c){char u[34],l[34],*C=" ┐┌──└┘ ",i=0;while(i<34)X(n&3),n>>=1,X((n&1)*3);printf("  %.33s\n%c %.33s\n",u,c,l);}main(){p(0xD5D4,'A');p(0x14EB6,'B');p(0x649C,'X');}

Ungolfed, define expanded, and commented:

p(n,c){
    // u is the upper line of the graph, l the lower line
    char u[34],l[34],*C=" ┐┌──└┘ ",i=0;
    while(i<34)
        u[i]=C[n&3],            // using the two LSBs to set the transition char depending on the current and next state
        l[i++]=C[(n&3)+4],      // do for both upper and lower lines
        n>>=1,                  // shift bits right to go to next state
        u[i]=C[(n&1)*3],        // using only the LSB to set the "steady" char depending on current state only
        l[i++]=C[((n&1)*3)+4];  // do for both upper and lower lines
    printf("  %.33s\n%c %.33s\n",u,c,l);
}
main() {
    // Call p for each graph
    // Constants are chosen so the display is consistent with the request.
    // Each bit represents a state, but the order is reversed
    // (leftmost is put on lowest significant bit, after a 0)
    p(0xD5D4,'A');p(0x14EB6,'B');p(0x649C,'X');
}

Note: the C string must not contain unicode characters. All displayable characters must be plain old 8-bit chars (but they may be chosen in the extended range). So basically, the validity of the output depends on your code page.

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

tcl, 221 chars, 299 bytes

lmap {b _ n u A V} {"   " ┌─────┐ ┌───┐ └───┘ ┌─┐ └─┘} {puts "[set S \ $b][set m $A\ $A] $_ $m $n
A ──┘ [set w $V\ $V][set s \ $S]$w $V$b└──
  $n $n $A $_$b$m
B ┘$b$V$b$w$s$u $V └
$S$_$b$A$b$A$b$n
X ──┘$s$u $u $u$b└────"}

can be run on: http://rextester.com/live/VVQU99270

\$\endgroup\$
  • \$\begingroup\$ 228 characters, however it is 306 bytes. \$\endgroup\$ – Magic Octopus Urn Jan 12 '17 at 20:33
  • \$\begingroup\$ @carusocomputing: What online tool do you use to count bytes instead of chars? Tks in advance. \$\endgroup\$ – sergiol Jan 14 '17 at 11:06
  • \$\begingroup\$ mothereff.in/byte-counter \$\endgroup\$ – Magic Octopus Urn Jan 14 '17 at 19:40

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.