2

Is it possible to create an algorithm to solve Vim-golf problems? For those not familiar with what that is, you are given two different blocks of text, and must transform the first block into the second using the lowest possible number of keystrokes (using Vim in the canonical example, or whatever text editing program you choose to use). My initial suspicion is that the answer is no; We know an upper bound on the number of text changes required - manually delete the differences and enter the correct text. However getting down to the minimum amount is more complicated - text editors can program powerful macros to perform tasks, and you can have compositions of multiple macros - I'm guessing that there might be some way to show a correspondence with the halting problem but I'm not quite sure of the details.

Andrew
  • 6,295
  • 11
  • 56
  • 95
  • How about: ``CTRL-A CTRL-C CTRL-TAB CTRL-A CTRL-V``. 5 keystrokes in ALL possible cases. Unless you count CTRL also as a key stroke... jadda jadda. – BitTickler Jul 14 '15 at 00:00
  • Well it's certainly not undecidable, because you can just try all possible sequences of keystrokes until you find the answer. There's an upper bound on the length, because you can just delete one and insert the other. And even if there is a reduction to some NP-hard problem, you are probably fine with a good approximation. If a human can do it, a computer should be able to do it even better. So is it doable? Probably, but it seems like a tought problem. A lot of non-trivial problems such as edit distance and string factorization seem to reduce to the Vim problem. – Niklas B. Jul 14 '15 at 00:01
  • That makes sense Niklas. – Andrew Jul 14 '15 at 00:02
  • Aside from my generic solution above, I feel it is a badly formed question. The editor itself does not describe a concise set of available operations. So anyone can always invent the "magic 1 key macro" which solves the problem. – BitTickler Jul 14 '15 at 00:03
  • @BitTickler for the sake of the problem, assume that no predefined macros exist - to solve the problem using it, it counts towards your strokes when composing the macro – Andrew Jul 14 '15 at 00:04
  • @BitTickler I think you're supposed to use a default Vim config, so any macro you define will count towards your input size. It's still very broad of course – Niklas B. Jul 14 '15 at 00:04
  • @NiklasB. Are there any examples of approximation algorithms that might be related to this? Also, can you add your comment as an answer so I can accept it? – Andrew Jul 14 '15 at 00:09
  • Default VIM has no copy pasta? :) Seriously. Even when saying "you have single character insert and delete operations and single char overwrite available", the question gets better than mentioning vim or something. Then usually you end up looking for information theoretical closesness of the 2 texts e.g. hamming distances which might lead to some estimate.But imagine the 2 texts: "123" and "321". Who would program a solution for that with the operations offered by the question? I still feel the question/problem could be improved. – BitTickler Jul 14 '15 at 00:10
  • @BitTickler: how about a rule - no copy pasting of the second block of text :). Reducing it to just insert, delete, overwrite takes away much of the interest of the problem, I think - the macro component/complicated commands available make it a more interesting solution space – Andrew Jul 14 '15 at 00:14
  • @Andrew Oh is that all you wanted to know? I was under the impression that there's the unspoken question of how you would solve something like this. – Niklas B. Jul 14 '15 at 00:19
  • @NiklasB. I would like to know that as well, but as you said you can try every combination of keys - so there does an exist an algorithm which answers my question. Alternatively, I can edit the question to include this information – Andrew Jul 14 '15 at 00:23
  • @Andrew You can also ask a new question. Although I would recommend restricting yourself to a very basic and well-defined subset of vim, such as insert, delete and macro definition/application. – Niklas B. Jul 14 '15 at 00:24

1 Answers1

3

EDIT: As was pointed out by WuTheFWasThat, VIM macros are turing complete, so the problem is most likely undecidable and hence the answer to your question is no. The reason my answer below is wrong is because it assumes we can decide in finite time whether a given sequence of keystrokes in VIM terminates, which is not the case.

Old answer

Well it's certainly not undecidable, because you can just try all possible sequences of keystrokes until you find the answer. There's an upper bound on the length, because you can just delete one and insert the other. And even if there is a reduction to some NP-hard problem, you are probably fine with a good approximation. If a human can do it, a computer should be able to do it even better.

So is it doable? Probably, but it seems like a tough problem.

Why? Because a lot of non-trivial problems such as edit distance, string factorization and countless compression algorithms seem to reduce to the Vim problem. A human can take the solutions for all of these problems and combine them creatively to arrive at a solution.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • You can't just "try all possible sequences of keystrokes". Simulating a set of keystrokes can't be done in bounded time because of macros (in vim, macros can recursively call each other, or even themselves). I suspect the correct answer is no. My instinct is to try and simulate the SKI calculus. Recursive vim macros break upon e.g. a failed search, so you can imagine how to implement the outer loop. – WuTheFWasThat Jan 07 '17 at 01:25
  • @WuTheFWasThat interesting, I didn't know that VIM macros are Turing complete when I wrote tgis answer. In that case the problem is probably not even decidable. Let's see if I can update the answer – Niklas B. Jan 07 '17 at 10:54
  • Any algorithm that evaluates candidate paths to the solution would do well to omit any path whose length (in keystrokes) is longer than the current best known path. This bounds the search space so severely that I think you could rule out pathways which require you to solve the halting problem for Turing machines. – MatrixManAtYrService May 28 '18 at 17:27