Add day 04
[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 (split "\\n" str)))
10
11 (defun cartesian-product (A B &rest C)
12 (let ((helper (lambda (X Y)
13 (if Y (loop for x in X
14 nconc (loop for y in Y
15 collecting (append x (list y))))
16 X)))
17 (wrap-A (mapcar #'list A)))
18 (reduce helper C :initial-value (funcall helper wrap-A B))))
19
20 (def-suite util)
21 (in-suite util)
22
23 (test cart-prod-2
24 (is (equal
25 '((a a) (a b) (b a) (b b))
26 (cartesian-product '(a b) '(a b)))))
27
28 (test cart-prod-3
29 (is (equal
30 '((a a a) (a a b) (a b a) (a b b)
31 (b a a) (b a b) (b b a) (b b b))
32 (cartesian-product '(a b) '(a b) '(a b)))))
33
34 (test cart-prod-4
35 (is (equal
36 '((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)
37 (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))
38 (cartesian-product '(a b) '(a b) '(a b) '(a b)))))
39
40