# HG changeset patch # User Steve Losh # Date 1512525299 18000 # Node ID 902b736dfedb4fc820bb9bb49604f80b2a868ba6 # Parent b50f1ec26d640df599735106609ff95213eeafd8 Refactor diff -r b50f1ec26d64 -r 902b736dfedb src/main.lisp --- a/src/main.lisp Tue Dec 05 18:16:40 2017 -0500 +++ b/src/main.lisp Tue Dec 05 20:54:59 2017 -0500 @@ -25,15 +25,6 @@ (collect (str:words line)))) -(defun contains-duplicates-p (list &key (test #'eql)) - (iterate (for (head . tail) :on list) - (thereis (member head tail :test test)))) - -(defun anagramp (string1 string2) - (string= (sort (copy-seq string1) #'char<) - (sort (copy-seq string2) #'char<))) - - ;;;; Problems ----------------------------------------------------------------- (defun day-1/1 () (iterate (for (x . y) :pairs-of-list (read-file-of-digits "data/2017/01")) @@ -96,20 +87,23 @@ (setf (gethash coord memory) value)))) -(defun d4-valid-passphrase-p (phrase) - (not (contains-duplicates-p phrase :test #'string=))) - (defun day-4/1 () - (count-if #'d4-valid-passphrase-p - (read-file-of-lines-of-words "data/2017/04"))) + (labels ((contains-duplicates-p (list &key (test #'eql)) + (iterate (for (head . tail) :on list) + (thereis (member head tail :test test)))) + (validp (phrase) + (not (contains-duplicates-p phrase :test #'string=)))) + (count-if #'validp (read-file-of-lines-of-words "data/2017/04")))) (defun day-4/2 () - (flet ((contains-anagram-p (phrase) - (iterate (for (word . tail) :on phrase) - (thereis (find-if (curry #'anagramp word) tail))))) - (-<> (read-file-of-lines-of-words "data/2017/04") - (remove-if-not #'d4-valid-passphrase-p <>) - (count-if-not #'contains-anagram-p <>)))) + (labels ((anagramp (string1 string2) + (string= (sort (copy-seq string1) #'char<) + (sort (copy-seq string2) #'char<))) + (contains-anagram-p (phrase) + (iterate (for (word . tail) :on phrase) + (thereis (find-if (curry #'anagramp word) tail))))) + (count-if-not #'contains-anagram-p + (read-file-of-lines-of-words "data/2017/04")))) (defun day-5/1 ()