Search HoldMyTwist

Browse all posts

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.

Episodes

Computing Basics

    Robot holds two torn ends of a plain navy scarf while Duck asks for an eight-cell repair patch.
    A bounded fabric repair template has exactly four navy cells, two gold cells and two navy cells, read from left to right.
    Duck counts the first four adjacent navy cells on the actual stitched eight-cell patch.
    Robot records NAVY4 on a repair ticket. The bounded patch has four adjacent navy cells before the two gold cells.
    Robot points to the two gold cells separating the first four navy cells from the final two navy cells.
    The repair ticket records NAVY4 followed by GOLD2, preserving the run order.
    Duck considers joining the two navy runs, but the gold pair remains visibly between them.
    Robot points to the separating gold pair in the actual bounded patch, and records the final NAVY2 run separately.
    Duck asks whether the patch can be rebuilt from a three-record ticket. The scarf is still torn and the reconstruction has not begun.
    Robot sews four navy cells into the eight-position repair space. Four positions remain empty.
    Two gold cells follow the four navy cells. The final two outlined positions are still empty.
    Robot finishes the last two navy cells. All eight physical patch cells now match NAVY4, GOLD2, NAVY2.
    Robot holds the repaired plain navy scarf with its bounded eight-cell patch fully visible; Duck celebrates.
    Robot reaches the end of the three-record repair ticket and stops.
    Duck presents a different, separate four-cell swatch: navy, gold, navy, gold.
    Each alternating cell needs its own color-and-count record. Robot lays out four separate records.
    A four-record ticket accompanies the short alternating swatch. Under the stated example format, four original bytes become eight encoded bytes.
    Robot compares the small swatch with its long ticket. In this example each record stores one color byte and one count byte. The first patch shrinks from 8 to 6 bytes; the alternating swatch grows from 4 to 8 bytes.
    The small alternating swatch is set aside. Robot returns to the original scarf with its repaired four-navy, two-gold, two-navy patch.
    Robot curls a paper ticket into a stiff little scarf shape; Duck laughs from inside the monitor.

    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)