Inspired by this video by Ben Eater. This challenge forms a pair with Decode USB packets.
The USB 2.0 protocol uses, at a low level, a line code called non-return-to-zero encoding (specifically, a variant called NRZI), in which a stream of bits is encoded into a stream of two electrical level states J
and K
. Encoding works as follows:
- Start with some initial state, which is either
J
orK
- For each bit to be encoded:
- If the bit is a 0, switch the state from
J
toK
or fromK
toJ
- If the bit is a 1, maintain the same state as before
- and output that state
- If the bit is a 0, switch the state from
For this challenge, we will assume the encoding starts in state J
.
However, it's not that simple: an additional process called bit stuffing takes place, which is designed to make it easier to detect if the signal has dropped out. After 6 consecutive 1
s are read, an extra meaningless 0
bit is processed to ensure the signal never stays in the same state (J
or K
) for too long.
The full USB specification is available here on the USB website, or mirrored (and easier to access) here, but I'd caution you before reading it because it's extremely long and the pertinent information is hard to pin down.
Task
Given a non-empty binary string as input, encode the string using the USB implementation of NRZI described above, and output a string of J
s or K
s.
Test-cases
Input Output
=====================================
1 J
0 K
1000101 JKJKKJJ
00000000 KJKJKJKJ
11111111 JJJJJJKKK
001111100 KJJJJJJKJ
0011111100 KJJJJJJJKJK
00111111100 KJJJJJJJKKJK
0011111111111100 KJJJJJJJKKKKKKKJKJ
0011011000111001 KJJJKKKJKJJJJKJJ
01101000011101000111010001110000011100110011101000101111001011110111011101110111011101110010111001111001011011110111010101110100011101010110001001100101001011100110001101101111011011010010111101110111011000010111010001100011011010000011111101110110001111010110010001010001011101110011010001110111001110010101011101100111010110000110001101010001 KKKJJKJKJJJJKKJKJJJJKKJKJJJJKJKJKKKKJKKKJKKKKJJKJKKJJJJJKJJKKKKKJJJJKKKKJJJJKKKKJJJJKKKKJKKJJJJKJJJJJKJJKKKJJJJJKKKKJJKKJJJJKKJKJJJJKKJJKKKJKJJKJJJKJJKKJKKJJJJKJJJKJKKKJJJKKKKKJJJKKKJJKJJKKKKKJJJJKKKKJJJKJKJJKKKKJJKJKKKJKJJJKKKJJKJKJKKKKKKKJKKKKJJJKJKKKKKJJKKKJKKJKJJKKJKJJKKKKJJJJKJJJKKJKJJJJKKKKJKKKKJKKJJKKJJJJKKKJKKKKJJKKKJKJKKKJKJJJKKJJKJKK
Rules
- The input should be a string or array of
1
s and0
s or oftrue
s andfalse
s - The output should be a string or array, with the two states
J
andK
being represented by any two distinct values - You may use any standard I/O method
- Standard loopholes are forbidden
- This is code-golf, so the shortest code in bytes wins
J
andK
, but may we take an input where0
meanstrue
and1
meansfalse
? \$\endgroup\$