Draw the XNOR digital timing diagram

12

2

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.

zeppelin

Posted 2017-01-06T13:26:38.800

Reputation: 7 884

2That's an XNOR gate, isn't it... – clismique – 2017-01-06T13:33:35.823

@Qwerp-Derp Yep, maybe it is :) – zeppelin – 2017-01-06T13:52:12.777

2Instead of suggesting (0==0)==0 shan't either B or X start and end at 1? – Roman Czyborra – 2017-01-06T14:11:33.667

@Roman Czyborra Not sure I understand the idea, could you elaborate on this a bit ? – zeppelin – 2017-01-09T10:14:25.830

1@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). – isaacg – 2017-01-09T11:19:13.470

@isaacg, only A and X are low, B is high, but as it starts with "light up and left" - , it makes it look like it transitions from low. This is a peculiarity of using ASCII art to represent the diagram. Starting with all 1es, would have probably made it look nicer, but that is too late for this kind of change. – zeppelin – 2017-01-09T13:23:37.437

@xnor how'd he get your selfie? – Magic Octopus Urn – 2017-01-12T20:44:12.253

Answers

5

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.

Magic Octopus Urn

Posted 2017-01-06T13:26:38.800

Reputation: 19 422

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. – zeppelin – 2017-01-12T21:28:14.010

I agree with zeppelin. Given the concession given in the rules, this would therefore be 106 bytes. – Level River St – 2017-01-12T21:54:59.393

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

– Kevin Cruijssen – 2019-08-12T09:18:56.887

4

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.

Anders Kaseorg

Posted 2017-01-06T13:26:38.800

Reputation: 29 242

3

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
}

Level River St

Posted 2017-01-06T13:26:38.800

Reputation: 22 049

This is actually 129 bytes, 113 characters. – Magic Octopus Urn – 2017-01-12T20:32:53.443

1@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. – Level River St – 2017-01-12T21:51:29.447

2

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.

AdmBorkBork

Posted 2017-01-06T13:26:38.800

Reputation: 41 581

You are allowed to count the unicode characters as single bytes, according to the challenge – FlipTack – 2017-01-08T10:10:23.633

2

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

Arnauld

Posted 2017-01-06T13:26:38.800

Reputation: 111 334

j%2?" ─"[p^i&1]:" ┐┌─└┘ "[p+(p=[27370,42843,12878][i>>1]>>j/2&1)*2+i%2*3] saves 3 bytes. – Neil – 2017-01-06T22:01:46.110

Oh, and change 17 to 33 too. – Neil – 2017-01-06T22:02:09.470

Once again, 152 characters, 164 bytes. – Magic Octopus Urn – 2017-01-12T20:33:48.007

2

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.

dim lost faith in SE

Posted 2017-01-06T13:26:38.800

Reputation: 7 018

1

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

sergiol

Posted 2017-01-06T13:26:38.800

Reputation: 3 055

228 characters, however it is 306 bytes. – Magic Octopus Urn – 2017-01-12T20:33:23.143

@carusocomputing: What online tool do you use to count bytes instead of chars? Tks in advance. – sergiol – 2017-01-14T11:06:57.493

https://mothereff.in/byte-counter – Magic Octopus Urn – 2017-01-14T19:40:34.093