(in-package #:adventofcode2020) ;;; Utility functions (defun list-from (str) (split "\\n" str)) (defun int-list-from (str) (mapcar #'parse-integer (list-from str))) (defun list-list-from (str) (split-sequence "" (list-from str) :test #'string=)) (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)))) (defun manhattan-distance (a b) (destructuring-bind ((ax ay) (bx by)) (list a b) (+ (abs (- ax bx)) (abs (- ay by))))) (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)))))