A rectangular paper sheet is drawn as a list of equal-length strings sheet, one character per square. The ink soaks through, so each square shows the same character on both faces. The sheet lies flat on a table; rows and columns are always counted in the current (folded) sheet as seen from above, with row 0 at the top and column 0 at the left. Every position of the folded sheet holds a stack of one or more paper layers.
Each entry of folds is a string "S k" where k is a positive integer smaller than the current size in that direction:
"L k": thekleftmost columns are lifted and folded over to the right along the line between columnk-1and columnk. That line becomes the new left edge: old columnk+jbecomes new columnj, and the flap's old columnk-1-jlands on new columnj. The new width ismax(k, width - k), so a flap wider than the rest sticks out past the old right edge."R k": the mirror image. Thekrightmost columns fold over to the left, the fold line becomes the new right edge, and the new width ismax(k, width - k)."U k": thektop rows fold down; the fold line becomes the new top edge; the new height ismax(k, height - k)."D k": thekbottom rows fold up; the fold line becomes the new bottom edge; the new height ismax(k, height - k).
A flap turns over as it folds, so it lands on top of whatever is already at its new position, and the order of its layers is reversed: at each position, the flap's lowest layer becomes the highest one.
After all folds, a needle is pushed through every layer at each position (r, c) in punches. Return a tuple (top, pierced):
top: the folded sheet seen from above, as a list of strings: at each position, the character of the highest layer.pierced: the sheet unfolded back to its original shape, as a list of strings, with every pierced square replaced by*.
Examples
Input: sheet = ["abc"], folds = ["L 1"], punches = [(0, 0)]
Output: (["ac"], ["**c"])
Explanation: column 0 folds onto column 1, so the new width is 2 and "a"
lies on top of "b". The needle at (0, 0) pierces both "b" and "a".
Input: sheet = ["abcd",
"efgh",
"ijkl"], folds = ["R 1", "U 2"], punches = [(0, 2)]
Output: (["efg", "abc"], ["abcd", "ef**", "ij**"])
Explanation: after "R 1" the sheet is 3 wide and "d", "h", "l" lie on "c",
"g", "k". Then the top two rows fold down onto the last row and the sheet is
2 high. At new position (0, 2) the stack from bottom to top is k, l, h, g:
the old stack g, h was turned over, so g is now highest.
Constraints
1 <= len(sheet), len(sheet[0]) <= 30; the characters are lowercase letters or.0 <= len(folds) <= 12, and every fold is valid for the current size0 <= len(punches) <= 10; every punch is a valid position in the final folded sheet
Goals
- Apply reflections of a grid about a fold line, including flaps that overhang the edge
- Track the stack of paper layers at every position and how folding reverses it
- Map positions in the folded sheet back to squares of the original sheet