Typewriter Carriage
MediumOnline Assessment
Replay keystrokes on a manual typewriter and report what ended up on the paper and how many times the margin bell rang.
The sheet has width columns numbered 0 to width - 1, and the carriage starts at column 0 of line 0. The carriage position p can be anywhere from 0 to width; at p = width the carriage is locked at the right margin. Process keys one character at a time:
- A letter, digit,
-or_is typed in columnpof the current line, then the carriage moves top + 1. If the cell was empty, it now holds that character. If it already holds the same character, nothing changes; if it holds a different one, the two overstrike and the cell becomes#. While the carriage is locked, typing does nothing. - A space moves the carriage to
p + 1without marking the paper (it never changes a cell). While locked, it does nothing. <(backspace) moves the carriage one column left, never below 0. It does not erase anything.- A tab character moves the carriage to the smallest tab stop in
tabsthat is greater thanp. If there is none, it moves towidthand locks. While locked, it does nothing. \r(carriage return) moves the carriage back to column 0 on the same line.\n(line feed) moves the paper up one line, keeping the carriage column.
The bell rings each time a typed character or space (not a tab) moves the carriage from column bell - 1 to column bell. Backspacing and passing that point again rings it again. Typing or spacing that is ignored because the carriage is locked never rings.
Input: width, bell, tabs and keys.
Output: [lines, bells]. lines is the page from line 0 down to the last line that has a mark on it, each as a string of the line's cells with trailing spaces removed (lines in between with no marks are empty strings). If nothing was ever marked, lines is []. bells is the number of rings.
Example 1
- Input:
- [12,8,[4,8],"ab\tcd\tx yz\t!"]
- Output:
- [["ab cd x yz"],0]
- Explanation
aandbfill columns 0-1, the tab jumps to stop 4,canddfill columns 4-5, and the next tab jumps from 6 to stop 8. Landing on the bell column by tab does not ring it, andxmoves the carriage from 8 to 9, which is not the bell step. After a space,yandzfill columns 10 and 11 and the carriage locks at 12, so the final tab and!are ignored. The page isab cd x yzwith 0 rings.
Example 2
- Input:
- [10,10,[],"total\r_____\n\rend"]
- Output:
- [["#####","end"],0]
- Explanation
totalis typed in columns 0-4, then\rreturns to column 0 and_____overstrikes every cell with a different character, giving#####. After the\nthe paper is on line 1 and the carriage is at column 5; the\rreturns it to column 0, soendstarts at column 0. The bell is at column 10, which this text never reaches, so there are 0 rings.
Example 3
- Input:
- [9,6,[3,3,7],"abcdefghijk<<<<xy\r\n ok"]
- Output:
- [["abcde##hi"," ok"],2]
- Explanation
abcdefis typed in columns 0-5; moving from column 5 to 6 rings the bell (1).ghifill columns 6-8 and the carriage locks at 9, sojandkare ignored. Four backspaces bring it to column 5, wherexoverstrikesfandyoverstrikesg, both becoming#; moving from 5 to 6 again rings the bell (2). After\r\nthe two spaces andokland in columns 2-3 of line 1.
Constraints
- 1 ≤ width ≤ 40; 1 ≤ bell ≤ width; 0 ≤ tabs.length ≤ 10 with 1 ≤ tabs[i] < width, in any order and possibly repeated
- 0 ≤ keys.length ≤ 200; keys contains lowercase letters, digits, space,
-,_,<, tab,\rand\n; the sheet never goes past line 100