Add day 06
[adventofcode2020.git] / src / day06.lisp
diff --git a/src/day06.lisp b/src/day06.lisp
new file mode 100644 (file)
index 0000000..e116865
--- /dev/null
@@ -0,0 +1,36 @@
+(asdf:load-system :adventofcode2020)
+(in-package #:adventofcode2020)
+(named-readtables:in-readtable fn-reader)
+
+(defun count-answers (join ans-list)
+  (->> ans-list
+    (mapcar λ(map 'list #'identity _))
+    (reduce λ(funcall join _0 _1 :test #'char=))
+    (length)))
+
+(day 06 input
+  (let ((answer-groups (-<>> (list-from input)
+                         (split-sequence "" <> :test #'string=)))
+        (disjunction (curry #'count-answers #'union))
+        (conjunction (curry #'count-answers #'intersection) ))
+    (part1 (->> answer-groups (mapcar disjunction) (reduce #'+)))
+    (part2 (->> answer-groups (mapcar conjunction) (reduce #'+)))))
+
+(def-suite day06)
+(in-suite day06)
+
+(test count-answers-disj
+  (is (equal
+        '(3 3 3 1 1)
+        (mapcar (curry #'count-answers #'union)  
+                '(("abc") ("a" "b" "c") ("ab" "ac") 
+                          ("a" "a" "a" "a") ("b"))))))
+
+(test count-answers-conj
+  (is (equal
+        '(3 0 1 1 1)
+        (mapcar (curry #'count-answers #'intersection)  
+                '(("abc") ("a" "b" "c") ("ab" "ac") 
+                          ("a" "a" "a" "a") ("b"))))))
+
+(run! 'day06)