Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created July 6, 2020 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinmeiners/b3bff6966c9f4501803e8e01d306b540 to your computer and use it in GitHub Desktop.
Save justinmeiners/b3bff6966c9f4501803e8e01d306b540 to your computer and use it in GitHub Desktop.
(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