]> localhost Git - adventofcode2020.git/blob - day06.lisp
2f58f7210a2c1eacf85f5e310fbac72272d1c3c8
[adventofcode2020.git] / day06.lisp
1 (asdf:load-system :adventofcode2020)
2 (in-package #:adventofcode2020)
3 (named-readtables:in-readtable :adventofcode2020)
4
5 (defun count-answers (join ans-list)
6 (->> ans-list
7 (mapcar λ(map 'list #'identity _))
8 (reduce λ(funcall join _0 _1 :test #'char=))
9 (length)))
10
11 (day 06 input
12 (let ((answer-groups (-<>> (list-from input)
13 (split-sequence "" <> :test #'string=)))
14 (disjunction (curry #'count-answers #'union))
15 (conjunction (curry #'count-answers #'intersection) ))
16 (part1 (->> answer-groups (mapcar disjunction) (reduce #'+)))
17 (part2 (->> answer-groups (mapcar conjunction) (reduce #'+)))))
18
19 (def-suite day06)
20 (in-suite day06)
21
22 (test count-answers-disj
23 (is (equal
24 '(3 3 3 1 1)
25 (mapcar (curry #'count-answers #'union)
26 '(("abc") ("a" "b" "c") ("ab" "ac")
27 ("a" "a" "a" "a") ("b"))))))
28
29 (test count-answers-conj
30 (is (equal
31 '(3 0 1 1 1)
32 (mapcar (curry #'count-answers #'intersection)
33 '(("abc") ("a" "b" "c") ("ab" "ac")
34 ("a" "a" "a" "a") ("b"))))))
35
36 (run! 'day06)