Episode 6
The Scarf That Fit on a Ticket
Duck: “My scarf needs an eight-cell patch.” There's a little ticket with color and length of each stripe.




















Episode transcript
A torn scarf
REPAIR PATCH: 8 CELLS IN A LONGER SCARF
Duck: My scarf needs an eight-cell patch.
Robot creates eight cells, with each cell being a square. Robot marks the left as the reading start. The required pattern is four navy cells, followed by two gold cells, followed by two navy cells.
START → N N N N G G N N
N = NAVY. G = GOLD
Robot: Read from this end.
Duck: Four navy cells together
Robot writes NAVY 4 (color/count) on a repair ticket.
TICKET: NAVY 4
Robot: One record for that run
Separate the runs
Duck: Now two gold.
Robot adds GOLD 2 after NAVY 4 on repair ticket.
TICKET: NAVY 4, GOLD 2
Robot: Keep their order.
Duck: Can all the navy go together?
The two gold squares are in between the navy stretches. If you connect the two navy stretches, the gold goes in the wrong position.
N N N N | G G | N N
Robot: Gold separates them. Another run.
Read the repair ticket
TICKET: NAVY 4, GOLD 2, NAVY 2
N N N N
Robot: Four navy first.
N N N N G G
Duck: Then the gold pair.
The row after rebuilding is navy navy navy navy gold gold navy navy.
N N N N G G N N
Robot: And 2 navy at the end
A pattern with short runs
REPAIRED FIRST PATCH: N N N N G G N N
Duck: It fits!
There are three records in the ticket.
3 RECORDS READ → STOP
Robot: It's all done. The ticket is over. Stop.
START → N G N G
Duck: What about this one?
NAVY 1, GOLD 1, NAVY 1, GOLD 1
Robot: This is a new run for each cell.
The repaired scarf
The four-cell sample shows next to it a four-record ticket. That's a long ticket.
TOY FORMAT: ORIGINAL COLOR = 1 BYTE; RECORD = COLOR BYTE + COUNT BYTE
FOUR-CELL ALTERNATING SWATCH: RAW 4 BYTES; FOUR RECORDS 8 BYTES
Duck: This one takes more space!
EACH ONE-CELL RUN STORES ITS COLOR AND AN EXTRA COUNT
Robot: You have to have room for counts.
Robot puts aside the individual, alternating swatch of four cells and goes back to the first scarf. The fixed patch has four navy cells, two gold, and two navy as per the original repair ticket. Duck observes from the monitor as Robot shows off the scarf with the finished patch.
RETURN TO FIRST REPAIRED PATCH: N N N N G G N N
Duck: My stripes made it through the shortcut.
Robot rolls up repair ticket and forms it into a small scarf shape next to the finished scarf.
Robot: The ticket is a bit scratchy to wear.
Run-length encoding
Run length encoding saves a counter for each run of the same symbol (or color). Here, the ticket saves color first and then the number of cells. A run breaks when a new color starts. For example, distinct runs of navy are not combined because it would move the positions of gold cells. Encoder and decoder must agree on direction and format.
Encoding can't guarantee a size reduction.
A format that can grow
For this particular case only let's use 1 byte for each original color cell. For each run record use 2 bytes, 1 byte for the color of the run, 1 byte for the count of the run.
FIRST PATCH: 8 CELLS × 1 BYTE = 8 BYTES. 3 RECORDS × 2 BYTES = 6 BYTES
ALTERNATING SWATCH: 4 CELLS × 1 BYTE = 4 BYTES. 4 RECORDS × 2 BYTES = 8 BYTES
All adjacent pairs have different colors. That means each run only has one cell. For those four squares you now have 4 additional count bytes.
The toy format does not include file headers.
COUNTS FIT ONE BYTE HERE. FILE HEADERS AND WRITTEN ENGLISH TICKET LETTERING ARE OUTSIDE THIS COMPARISON.
Try a new patch
START → NAVY NAVY GOLD GOLD GOLD NAVY
Answer
NAVY 2, GOLD 3, NAVY 1 → N N G G G N
Starting from the left end indicated, it's NAVY 2, GOLD 3, NAVY 1. The order is deciphered by two navy then three gold then one navy. End after the last record. There's a total of six cells in the original order.
Sources
Computer Science Field Guide, Run length encoding (opens in a new tab)