X-Git-Url: http://git.jkinsey.net/?p=adventofcode2020.git;a=blobdiff_plain;f=src%2Futilities.lisp;h=91e74d11b1aeb5a8a6316c11562e7934f9eb0aa1;hp=c677e964c2cda5fd874d39b7c06b09586a0013c7;hb=a7a78c7ac10e6bb6245dcdeab826f118ca2c60b0;hpb=3ef62a9d5cf95c218e80455021e69d3bf5bf1541 diff --git a/src/utilities.lisp b/src/utilities.lisp index c677e96..91e74d1 100644 --- a/src/utilities.lisp +++ b/src/utilities.lisp @@ -6,4 +6,35 @@ (split "\\n" str)) (defun int-list-from (str) - (mapcar #'parse-integer (split "\\n" str))) + (mapcar #'parse-integer (list-from str))) + +(defun cartesian-product (A B &rest C) + (let ((helper (lambda (X Y) + (if Y (loop for x in X + nconc (loop for y in Y + collecting (append x (list y)))) + X))) + (wrap-A (mapcar #'list A))) + (reduce helper C :initial-value (funcall helper wrap-A B)))) + +(def-suite util) +(in-suite util) + +(test cart-prod-2 + (is (equal + '((a a) (a b) (b a) (b b)) + (cartesian-product '(a b) '(a b))))) + +(test cart-prod-3 + (is (equal + '((a a a) (a a b) (a b a) (a b b) + (b a a) (b a b) (b b a) (b b b)) + (cartesian-product '(a b) '(a b) '(a b))))) + +(test cart-prod-4 + (is (equal + '((a a a a) (a a a b) (a a b a) (a a b b) (a b a a) (a b a b) (a b b a) (a b b b) + (b a a a) (b a a b) (b a b a) (b a b b) (b b a a) (b b a b) (b b b a) (b b b b)) + (cartesian-product '(a b) '(a b) '(a b) '(a b))))) + +