Commit | Line | Data |
---|---|---|
3ef62a9d JK |
1 | (in-package #:adventofcode2020) |
2 | ||
3 | ;;; Utility functions | |
4 | ||
5 | (defun list-from (str) | |
6 | (split "\\n" str)) | |
7 | ||
8 | (defun int-list-from (str) | |
a7a78c7a | 9 | (mapcar #'parse-integer (list-from str))) |
63a814f2 | 10 | |
d0fc5e86 JK |
11 | (defun list-list-from (str) |
12 | (split-sequence "" (list-from str) :test #'string=)) | |
13 | ||
63a814f2 JK |
14 | (defun cartesian-product (A B &rest C) |
15 | (let ((helper (lambda (X Y) | |
16 | (if Y (loop for x in X | |
17 | nconc (loop for y in Y | |
18 | collecting (append x (list y)))) | |
19 | X))) | |
20 | (wrap-A (mapcar #'list A))) | |
21 | (reduce helper C :initial-value (funcall helper wrap-A B)))) | |
22 | ||
64665021 JK |
23 | (defun manhattan-distance (a b) |
24 | (destructuring-bind ((ax ay) (bx by)) (list a b) | |
25 | (+ (abs (- ax bx)) | |
26 | (abs (- ay by))))) | |
27 | ||
63a814f2 JK |
28 | (def-suite util) |
29 | (in-suite util) | |
30 | ||
31 | (test cart-prod-2 | |
32 | (is (equal | |
33 | '((a a) (a b) (b a) (b b)) | |
34 | (cartesian-product '(a b) '(a b))))) | |
35 | ||
36 | (test cart-prod-3 | |
37 | (is (equal | |
38 | '((a a a) (a a b) (a b a) (a b b) | |
39 | (b a a) (b a b) (b b a) (b b b)) | |
40 | (cartesian-product '(a b) '(a b) '(a b))))) | |
41 | ||
42 | (test cart-prod-4 | |
43 | (is (equal | |
44 | '((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) | |
45 | (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)) | |
46 | (cartesian-product '(a b) '(a b) '(a b) '(a b))))) | |
47 | ||
48 |