Add day 07
[adventofcode2020.git] / src / utilities.lisp
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)
9 (mapcar #'parse-integer (list-from str)))
10
11 (defun list-list-from (str)
12 (split-sequence "" (list-from str) :test #'string=))
13
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
23 (def-suite util)
24 (in-suite util)
25
26 (test cart-prod-2
27 (is (equal
28 '((a a) (a b) (b a) (b b))
29 (cartesian-product '(a b) '(a b)))))
30
31 (test cart-prod-3
32 (is (equal
33 '((a a a) (a a b) (a b a) (a b b)
34 (b a a) (b a b) (b b a) (b b b))
35 (cartesian-product '(a b) '(a b) '(a b)))))
36
37 (test cart-prod-4
38 (is (equal
39 '((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)
40 (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))
41 (cartesian-product '(a b) '(a b) '(a b) '(a b)))))
42
43