Layered ASCII Canvas Command Processor
Learn this problemProblem statement
Process a finite ordered list of commands over a canvas with width 10 and height 6. Coordinates are zero-based: x increases from left to right and y increases from top to bottom. The background character is ..
The supported commands are:
DRAW_RECTANGLE left top right bottom ch: create a filled inclusive rectangle using the single uppercase characterch. A new rectangle is the frontmost layer.ERASE_AREA left top right bottom: permanently erase every cell of every existing rectangle whose current canvas coordinate is inside the inclusive area. Erasure affects hidden cells too. A rectangle retains its erased local-cell mask when it later moves.DRAG_AND_DROP fromX fromY toX toY: select the frontmost non-erased rectangle cell visible at the source coordinate and translate its rectangle so the selected local point lands on the destination coordinate. The rectangle keeps its layer position. If no rectangle is visible at the source, do nothing.BRING_TO_FRONT x y: move the frontmost non-erased rectangle visible at that coordinate to the frontmost layer. If no rectangle is visible there, do nothing.PRINT_CANVAS: render the current canvas and append one snapshot to the answer.
Each snapshot joins its six top-to-bottom rows with /. At every cell, render the character of the frontmost rectangle whose corresponding local cell has not been erased; otherwise render .. Return snapshots in PRINT_CANVAS order.
Function
processCanvas(commands: String[]) → String[]Examples
Example 1
commands = ["DRAW_RECTANGLE 1 1 4 3 A","PRINT_CANVAS","DRAW_RECTANGLE 3 0 6 2 B","PRINT_CANVAS","ERASE_AREA 4 1 5 1","PRINT_CANVAS"]return = ["........../.AAAA...../.AAAA...../.AAAA...../........../..........","...BBBB.../.AABBBB.../.AABBBB.../.AAAA...../........../..........","...BBBB.../.AAB..B.../.AABBBB.../.AAAA...../........../.........."]The second rectangle covers part of the first. Erasing columns 4 through 5 on row 1 removes those cells from both rectangles, including the hidden cell of the first rectangle.
Example 2
commands = ["DRAW_RECTANGLE 1 1 2 2 X","DRAG_AND_DROP 1 1 5 3","PRINT_CANVAS","ERASE_AREA 5 3 5 3","DRAG_AND_DROP 6 4 7 5","PRINT_CANVAS"]return = ["........../........../........../.....XX.../.....XX.../..........","........../........../........../........../.......X../......XX.."]The first drag translates the rectangle by [4,2]. After its top-left local cell is erased, a second drag moves the remaining shape together with that permanent hole.
Constraints
1 <= commands.length <= 1000.- Every command has exactly the syntax shown above and integer coordinates within the canvas.
- Every drawn rectangle satisfies
0 <= left <= right < 10and0 <= top <= bottom < 6. - Every drag that selects a rectangle keeps that rectangle fully inside the canvas after translation.
- Every
chis one uppercase English letter. - At most
200rectangles are created.