Created
July 14, 2025 09:11
-
-
Save enderahmetyurt/cb38fb76a640da43bb905e7d947d424e to your computer and use it in GitHub Desktop.
Given a multi-line string and a sequence of Vim navigation commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# h for left, j for down, k for up, and l for right) | |
def get_char_after_vim_commands(string, commands) | |
lines = string.lines.map(&:chomp) | |
row, col = 0, 0 | |
commands.each_char do |cmd| | |
dr, dc = {"h" => [0, -1], "j" => [1, 0], "k" => [-1, 0], "l" => [0, 1]}[cmd] || [0, 0] | |
row = (row + dr).clamp(0, lines.size - 1) | |
col = (col + dc).clamp(0, [lines[row].size - 1, 0].max) | |
end | |
lines[row][col] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment