Created
July 6, 2020 00:39
-
-
Save justinmeiners/b3bff6966c9f4501803e8e01d306b540 to your computer and use it in GitHub Desktop.
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
(defun group-adjacent (list &key (test #'eql)) | |
(( | |
(lambda (group x) | |
(if (or (funcall test (car group) x) (funcall test x (car group))) | |
(progn | |
(push group result) | |
(list x)) | |
(cons x group))) | |
(cdr list) | |
:initial-value (list (car list))) | |
(nreverse result))) | |
(defun compare-letters (a b) | |
(< (abs a) (abs b))) | |
(defun total-letters (group) | |
(reduce #'+ (mapcar #'signum group))) | |
(defun sum-distinct (word) | |
(reduce #'+ | |
(mapcar #'abs | |
(mapcar #'total-letters | |
(group-adjacent (sort (copy-list word) #'compare-letters) :test #'compare-letters))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment