A pixel-art editor applies drawing commands to a grid, supports undo and redo, and can report how many separate shapes are on the canvas.
Write run_canvas(rows, cols, commands) that starts from an empty rows x cols canvas, processes commands in order and returns a list with one entry per "count" command.
Commands are tuples:
("paint", r, c, color): set cell (r, c) to color (a non-empty string).("erase", r, c): make cell (r, c) empty.("rect", r1, c1, r2, c2, color): paint every cell with r1 <= r <= r2 and c1 <= c <= c2. The whole rectangle is one command.("undo",): revert the most recent drawing command that has not been undone. Does nothing if there is none.("redo",): re-apply the most recently undone command. Does nothing if there is none.("count",): append the number of shapes to the output. A shape is a maximal group of non-empty cells of the same color connected through up, down, left or right neighbours.Every paint, erase and rect is a drawing command, even if it changes nothing, and issuing one clears the redo history. Coordinates are always inside the canvas. rows, cols , up to commands.