"""PIXEL FORGE: make an alien, undo mistakes, then shrink its data.

Two lessons. No imports, installs or live input needed.
Run once to see the sample. Implement the four TODO functions, then
uncomment their checkpoints at the bottom. Save your own .py copy.
Lesson 1: pixels and undo. Lesson 2: lossless compression.
Every Run starts again from START_ROWS, so keep your design in the code.
"""

START_ROWS = [
    "00000000",
    "00100100",
    "01111110",
    "11011011",
    "11111111",
    "10100101",
    "00100100",
    "01000010",
]


# These helpers are supplied. Read them, but you need not rewrite them.
def render(pixels):
    for row in pixels:
        print("".join("##" if pixel == 1 else "  " for pixel in row))


def copy_pixels(pixels):
    # Copy each row too: changing the picture must not change its history.
    return [row[:] for row in pixels]


def flatten(pixels):
    # Read left to right, top to bottom; keep 0s as well as 1s.
    return "".join(str(pixel) for row in pixels for pixel in row)


def toggle_pixel(pixels, row, column, history):
    # TODO 1: change one pixel, remembering the image BEFORE the change.
    # Coordinates are integers, starting at 0. Return True for a valid edit.
    # Outside the image? Return False; change neither pixels nor history.
    # Hints: copy_pixels() makes a snapshot. How does a stack save an item?
    # How can you switch 0 to 1 and 1 to 0? Beware negative Python indices!
    raise NotImplementedError("TODO 1: implement toggle_pixel")


def undo(pixels, history):
    # TODO 2: return the most recent saved image, removing it from history.
    # If no saved image exists, return pixels unchanged.
    # Hint: which end of a stack gives LIFO? Do not make a new snapshot here.
    # The caller must keep the returned image: pixels = undo(pixels, history).
    raise NotImplementedError("TODO 2: implement undo")


def encode_rle(bits):
    # TODO 3: turn a binary string (at most 64 bits) into (bit, count) pairs.
    # Example: "00011" becomes [("0", 3), ("1", 2)]. Empty input gives [].
    # Hints: what do you remember while a run grows? What ends that run?
    # When the loop finishes, is there still a final run to record?
    raise NotImplementedError("TODO 3: implement encode_rle")


def decode_rle(runs):
    # TODO 4: turn the pairs back into the ORIGINAL binary string.
    # Each count is positive. Empty input gives "". Keep the original order.
    # Hint: how many copies of each bit belong in your result?
    raise NotImplementedError("TODO 4: implement decode_rle")


if __name__ == "__main__":
    pixels = [[int(bit) for bit in row] for row in START_ROWS]
    history = []
    print("PIXEL FORGE: your starting image")
    render(pixels)

    # LESSON 1 CHECKPOINTS: uncomment after implementing the functions.
    # toggle_pixel(pixels, 0, 0, history)
    # render(pixels)
    # pixels = undo(pixels, history)
    # render(pixels)
    # Add your own tests: a corner, invalid coordinates, two edits/two undos,
    # and undo with an empty history. Predict each result before running.
    # Make an original 8 x 8 design by editing START_ROWS; use only 0 and 1.

    # LESSON 2 CHECKPOINTS: uncomment after implementing the functions.
    # assert encode_rle("00011") == [("0", 3), ("1", 2)]
    # raw = flatten(pixels)
    # runs = encode_rle(raw)
    # print("Runs:", runs)
    # assert decode_rle(runs) == raw
    # Test empty input, all zeros and alternating "01" pixels as well.
    # Check a known expected encoding too: two wrong functions can agree!

    # COMPRESSION CHALLENGE: design a better AND a worse case for RLE.
    # Our imagined packed format: 1 pixel bit + 8 count bits PER RUN.
    # Counts are 1..64 here. Compare raw pixel bits with 9 * number of runs.
    # Print both totals for your design, 64 zeros and "01" repeated 32 times.
    # Exclude dimensions/headers. These are PAYLOAD BIT COUNTS, not Python
    # memory usage or the byte size of a text file containing "0" and "1".
    # Finish: explain why a lossless method can make some images larger.
