Apply a Cold Compress

Interview

Time limit1sMemory limit128 MB

Summary
Decode each compression-expression into its smallest pixel picture by parsing split tags and computing the relative scaling of the two halves, then draw the framed grid.
Level

Medium7 of 10

Topics
Recursion, Divide and conquer, Implementation, Math
Solved
No attempts yet

Problem

Many digital-image compression techniques work by finding and describing regions of a single uniform color. Here is a simple scheme for compressing black-and-white images (it extends easily to color). The idea is to repeatedly split the picture in half — either vertically or horizontally — until every sub-picture contains just one color.

A rectangular image is described by a compression-expression, defined as follows. Every compression-expression begins with a two-bit tag, which may be followed by more compression-expressions depending on the tag:

  • 00 — a square region of entirely black pixels. Depending on context this square may be a single pixel, a 2×22\times2 square, a 3×33\times3 square, and so on.
  • 11 — a square region of entirely white pixels, again of any size depending on context.
  • 10 — a horizontal split, followed by two compression-expressions. The picture is formed by placing the first picture on the left and the second on the right. A horizontal split is only possible between two pictures of the same height.
  • 01 — a vertical split, followed by two compression-expressions. The picture is formed by placing the first picture on top and the second underneath. A vertical split is only possible between two pictures of the same width.

When interpreting a split, the two components may need to be rescaled so that their shared dimension matches. For example, given a 2×62\times6 picture AA (2 wide, 6 tall) and a 3×43\times4 picture BB:

  • A vertical split is possible only if we scale AA by 3 (to 6×186\times18) and BB by 2 (to 6×86\times8); the combined picture is then 6×266\times26.
  • A horizontal split is possible only if we scale AA by 2 (to 4×124\times12) and BB by 3 (to 9×129\times12); the combined picture is then 13×1213\times12.

Using X for a black pixel and a space for a white pixel, the expression 00 denotes

---
|X|
---

and the expression 1000010011 denotes

-----
|XXX|
|XX |
-----

For any compression-expression there is a unique smallest picture it can denote; the same expression can also denote pictures twice that size, three times that size, and so on.

Input

Each line of input contains one compression-expression: a single line of an arbitrary number of 0s and 1s. Input continues until end of file.

Every expression in the input is a valid compression-expression.

Output

For each input expression, print the smallest black-and-white picture it denotes, drawing black pixels as X and white pixels as spaces, and framing it with - and | characters exactly as in the pictures above. Print nothing — no characters and no extra whitespace — outside the frame, apart from the newline that ends each line.

Your output must contain no blank lines.

Examples3

  1. Example 1

    Input
    00
    10001011100100101110111101111000100011
    
    Expected output
    ---
    |X|
    ---
    ----------------
    |XXXX    XXX   |
    |XXXX    XXX   |
    |XXXX    XXX   |
    |XXXX       XX |
    ----------------
    
  2. Example 2

    Input
    11
    
    Expected output
    ---
    | |
    ---
    
  3. Example 3

    Input
    1000010011
    
    Expected output
    -----
    |XXX|
    |XX |
    -----