]> localhost Git - adventofcode2020.git/commitdiff
Add day 01
authorJack Kinsey <j.jameskinsey@gmail.com>
Tue, 1 Dec 2020 22:36:25 +0000 (17:36 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Tue, 1 Dec 2020 22:36:25 +0000 (17:36 -0500)
package.lisp
src/adventofcode2020.lisp
src/day01.lisp [new file with mode: 0644]
src/utilities.lisp

index 363adb0ec5d0f20b16ce153eabaf2098b3683a56..6a6f94b5ae4c3cc9bffcfe4dd432c0daea1d9445 100644 (file)
@@ -1,3 +1,4 @@
+; (ql:quickload :fiveam) (ql:quickload :cl-ppcre)
 (defpackage #:adventofcode2020
   (:use #:cl 
         #:arrow-macros
index 54a6aca5cb4a42e17a76eae9bc1dc9837f1be850..4eaba0666294028822a84e9f3c12e33e66d00905 100644 (file)
@@ -6,8 +6,8 @@
        ,@body)))
 
 (defun part1 (str)
-  (format t "Part 1: ~A~%" str))
+  (time (format t "Part 1: ~A~%" str)))
 
 (defun part2 (str)
-  (format t "Part 2: ~A~%" str))
+  (time (format t "Part 2: ~A~%" str)))
 
diff --git a/src/day01.lisp b/src/day01.lisp
new file mode 100644 (file)
index 0000000..adb36e6
--- /dev/null
@@ -0,0 +1,29 @@
+(in-package #:adventofcode2020)
+
+(defun calculate-expenses (&rest reports)
+  (let ((prod (apply #'cartesian-product reports)))
+    (loop for tuple in prod
+          when (= (apply #'+ tuple) 2020)
+          return (apply #'* tuple))))
+
+(day 01 input
+  (let ((report (int-list-from input)))
+    (part1 (calculate-expenses report report))
+    (part2 (calculate-expenses report report report))))
+
+(def-suite day01)
+(in-suite day01)
+
+(test simple-expenses
+  (is (equal
+        514579
+        (let ((report '(1721 979 366 299 675 1456)))
+          (calculate-expenses report report)))))
+
+(test complex-expenses
+  (is (equal
+        241861950
+        (let ((report '(1721 979 366 299 675 1456)))
+          (calculate-expenses report report report)))))
+
+(run! 'day01)
index c677e964c2cda5fd874d39b7c06b09586a0013c7..ab6c3e8faa36eb6d59ac780c1ae7d8f6670a142b 100644 (file)
@@ -7,3 +7,34 @@
 
 (defun int-list-from (str)
   (mapcar #'parse-integer (split "\\n" 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)))))
+
+