From: Jack Kinsey Date: Fri, 4 Dec 2020 05:00:40 +0000 (-0500) Subject: Add day 02 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=d4ba41f7a606fcd69686fc6b850eb1e975bd9824;p=adventofcode2020.git Add day 02 --- diff --git a/src/day02.lisp b/src/day02.lisp new file mode 100644 index 0000000..fb93f03 --- /dev/null +++ b/src/day02.lisp @@ -0,0 +1,47 @@ +(in-package #:adventofcode2020) + +(defun valid-password-count? (pw-spec) + (destructuring-bind (count-str char-str passwd) (split " " pw-spec) + (let ((countp (destructuring-bind (count-low count-high) (mapcar #'parse-integer (split "-" count-str)) + (lambda (n) + (<= count-low n count-high)))) + (char-count (loop with char = (char char-str 0) + for c across passwd + summing (if (char= c char) 1 0)))) + (funcall countp char-count)))) + +(defun valid-password-index? (pw-spec) + (destructuring-bind (index-str char-str passwd) (split " " pw-spec) + (let* ((indices (mapcar (compose #'1- #'parse-integer) (split "-" index-str))) + (char (char char-str 0)) + (get-char (lambda (n) (char passwd n)))) + (->> (mapcar get-char indices) + (remove char) + (length) + (= 1))))) + +(day 02 input + (let ((pws-and-specs (list-from input))) + (part1 (->> pws-and-specs + (remove-if-not #'valid-password-count?) + (length))) + (part2 (->> pws-and-specs + (remove-if-not #'valid-password-index?) + (length))))) + +(def-suite day02) +(in-suite day02) + +(test valid-password-count? + (is (equal + '(t nil t) + (mapcar #'valid-password-count? + '("1-3 a: abcde" "1-3 b: cdefg" "2-9 c: ccccccccc"))))) + +(test valid-password-index? + (is (equal + '(t nil nil) + (mapcar #'valid-password-index? + '("1-3 a: abcde" "1-3 b: cdefg" "2-9 c: ccccccccc"))))) + +(run! 'day02) diff --git a/src/dayNN.lisp b/src/dayNN.lisp new file mode 100644 index 0000000..5b5dd9b --- /dev/null +++ b/src/dayNN.lisp @@ -0,0 +1,16 @@ +(in-package #:adventofcode2020) + +(day 00 input + (let ((modules (int-list-from input))) + (part1 ) + (part2 ))) + +(def-suite day00) +(in-suite day00) + +(test simple-fuel + (is (equal + '(2 2 654 33583) + (mapcar #'simple-fuel '(12 14 1969 100756))))) + +(run! 'day00)