| Bytes | Lang | Time | Link |
|---|---|---|---|
| 819 | Elisp | 240807T133429Z | Samuel J |
Elisp 819 bytes
This was a fun challenge. Not quite finished because doesn't use backspace. Most test cases pass. Use this to make your auto-type bot seem more human :)
Ungolfed
#!/usr/bin/emacs --script
(require 'seq)
;; source: https://emacs.stackexchange.com/questions/7148/get-all-regexp-matches-in-buffer-as-a-list
(defun re-seq (regexp string)
"Get a list of all regexp matches in a string"
(save-match-data
(let ((pos 0)
matches)
(while (string-match regexp string pos)
(push (match-string 0 string) matches)
(setq pos (match-end 0)))
matches)))
(defun how-quickly-can-you-type-this-string (s)
;; get all possible substrings, save right remainder
(let ((substrings nil))
(cl-loop for i to (length s) do
(setq substrings
(append
(cl-loop for j from i to (1-(length s))
when (not (equal j i))
collect
(list
(substring s i j)
(substring s j (length s))
i)
)
substrings
)
)
)
;; count number of matches in right remainder
(setq substrings
(cl-loop for ss in substrings collect
(cons (length (re-seq (car ss) (cadr ss)))
ss
)
)
)
;; calculate keystrokes
(princ
(car
(sort
(cl-loop for ss in substrings collect
(let ((keystrokes (+(cadddr ss)(length(cadr ss)))))
;; ctrl + c if matches exist
(if (> (car ss) 0)
(setq keystrokes (+ 2 keystrokes))
)
(while (> (car ss) 0)
(let ((match-index (string-match (cadr ss) (caddr ss))))
;; add characters preceding match as keystrokes and 1 for paste
(setq keystrokes (+ keystrokes match-index 1))
(setf (car ss) (1- (car ss)))
(setf (caddr ss) (substring (caddr ss) (+ match-index (length (cadr ss))) (length (caddr ss))))
)
)
;; add remaining characters as keystrokes
(setq keystrokes (+ keystrokes (length (caddr ss))))
)
)
#'<)
)
)
)
)
(how-quickly-can-you-type-this-string (pop argv))
(require 'seq)(defun r (regexp string)(save-match-data(let ((pos 0)matches)(while (string-match regexp string pos)(push (match-string 0 string) matches)(setq pos (match-end 0)))matches)))(defun q (s)(let ((x nil))(cl-loop for i to (length s) do(setq x(append(cl-loop for j from i to (1-(length s))when (not (equal j i))collect(list(substring s i j)(substring s j (length s))i))x)))(setq x(cl-loop for z in x collect(cons (length (r (car z) (cadr z)))z)))(princ(car(sort(cl-loop for z in x collect(let ((k (+(cadddr z)(length(cadr z)))))(if (> (car z) 0)(setq k (+ 2 k)))(while (> (car z) 0)(let ((m (string-match (cadr z) (caddr z))))(setq k (+ k m 1))(setf (car z) (1- (car z)))(setf (caddr z) (substring (caddr z) (+ m (length (cadr z))) (length (caddr z))))))(setq k (+ k (length (caddr z))))))#'<)))))(q (pop argv))