We all know that programmers tend to be lazy. In order to maximize your free time, you decide to write a program that outputs a minimal number of keystrokes for text fed into it.
Input: Text that has to be converted into keystrokes. You may decide on how to input the text (STDIN / reading from a file provided in the arguments)
Output: The necessary actions in the following format:
- They must be numbered
H
it: Pressing a key and immediately releasing itP
ress: Pressing a key and not releasing it (this will never be optimal when the key isR
eleased as the next keystroke)R
elease: Releasing aP
ressed key
Example:
Input:
Hello!
Output:
A naive solution would be:
1 P Shift
2 H h
3 R Shift
4 H e
5 H l
6 H l
7 H o
8 P Shift
9 H 1
10 R Shift
This would be more efficient:
1 P Shift
2 H h
3 H 1
4 R Shift
5 H Left
6 H e
7 H l
8 H l
9 H o
Environment:
- The editor uses a monospaced font
- Text is soft wrapped at 80 characters
- Arrow up and Arrow down preserve the column, even if there are shorter lines in between
- The clipboard is assumed to be empty
- Num lock is assumed to be enabled
- Caps lock is assumed to be disabled
- Caps lock only works for the letters (i.e. no Shift Lock)
Hotkeys / Shortcuts:
- Home: Jump to the beginning of the current line
- End: Jump to the end of the current line
- Ctrl+A: Mark everything
- Ctrl+C: Copy
- Ctrl+X: Cut
- Ctrl+V: Paste
- Shift+Cursor moving: Marking
- Ctrl+F: Opens a search dialog.
- Stupid text matching, no Regular Expressions
- Case sensitive
- Searches wrap around
- Single line text input for the search
- The input is prefilled with the current selection, unless there is a newline in between, the complete input is selected
- Copying / Pasting works as usual
- Pressing Enter performs the search, selecting the first match after the current cursor position
- F3: Repeat last search
- Ctrl+H: Opens a replace dialog
- Stupid text matching, no Regular Expressions
- Case sensitive
- Replace All, with wrap around
- Single line text inputs
- The search input is prefilled with the current selection, unless there is a newline in between, the complete input is selected
- The replace input is empty
- Copying / Pasting works as usual
- Tab jumps to the replace input
- Pressing Enter performs the replace all. The cursor is placed after the last replacement
Rules:
- Solutions must be a complete program that compiles / parses and executes without any further modification
- The keyboard displayed above is the keyboard to use
- It is not required to handle characters that cannot be typed with it
- Every key must be released at the end
- The cursor does not need to be at the end of file at the end
Scoring:
Your score is sum the amount of actions needed to type the following texts. The winner is the solution with the lowest score. Using my naive solution I get 1371 + 833 + 2006 = 4210
. Beat it! I will pick a winner in two weeks.
1 My naive solution
number = 1
H = (char) -> console.log "#{number++} H #{char}"
P = (char) -> console.log "#{number++} P #{char}"
R = (char) -> console.log "#{number++} R #{char}"
strokes = (text) ->
shiftActive = no
for char in text
if /^[a-z]$/.test char
if shiftActive
R "Shift"
shiftActive = no
H char
else if /^[A-Z]$/.test char
unless shiftActive
P "Shift"
shiftActive = yes
H char.toLowerCase()
else
table =
'~': '`'
'!': 1
'@': 2
'#': 3
'$': 4
'%': 5
'^': 6
'&': 7
'*': 8
'(': 9
')': 0
'_': '-'
'+': '='
'|': '\\'
'<': ','
'>': '.'
'?': '/'
':': ';'
'"': "'"
'{': '['
'}': ']'
if table[char]?
unless shiftActive
P "Shift"
shiftActive = yes
H table[char]
else
H switch char
when " " then "Space"
when "\n" then "Enter"
when "\t" then "Tab"
else
if shiftActive
R "Shift"
shiftActive = no
char
R "Shift" if shiftActive
input = ""
process.stdin.on 'data', (chunk) -> input += chunk
process.stdin.on 'end', -> strokes input
2 Easy repetition
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
3 More complex repetition
We're no strangers to love
You know the rules and so do I
A full commitment's what I'm thinking of
You wouldn't get this from any other guy
I just wanna tell you how I'm feeling
Gotta make you understand
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
We've known each other for so long
Your heart's been aching but
You're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it
And if you ask me how I'm feeling
Don't tell me you're too blind to see
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
(Ooh, give you up)
(Ooh, give you up)
(Ooh)
Never gonna give, never gonna give
(Give you up)
(Ooh)
Never gonna give, never gonna give
(Give you up)
We've know each other for so long
Your heart's been aching but
You're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it
I just wanna tell you how I'm feeling
Gotta make you understand
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
You can use the replay program written by me to test your solutions (Note: It does not support Searching / Replacing yet, everything else should work).