phlip wrote:Derek wrote:It's also very easy to write an encoder or decoder from one representation to the other.
This is the bit that I disagree with... sure, you can write a thing that goes through and reads R's and B's and writes RR's and RB's, but how does it know when it's done?
Fine, do this: R -> RRR, B -> RRB, G -> RBR, Y -> RBB, meta-marker -> B
A valid encoded string contains characters of 3 bits each, and the first bit is always R.
To encode a string:
1. Write a meta-marker (B).
2. If the next bit is B, consume it and finish. Otherwise, go to (3).
3. Read the next two bits and write out R, B, G, or Y as appropriate.
4. Go to (2).
To create a RB branch (GY is symmetric):
1. Write a meta-marker (B).
2. Read the next three bits. If RRR, exit 1, if RRB, exit 2. Otherwise, write the bits back and go to (3).
3. If the next bit is B, consume it and exit 3. Otherwise, copy the next three bits and go to (3).