X-Git-Url: http://git.jkinsey.net/?p=adventofcode2020.git;a=blobdiff_plain;f=src%2Fday05.lisp;fp=src%2Fday05.lisp;h=ee60cd2e80b036429559e35dc0b616a1d4f79253;hp=0000000000000000000000000000000000000000;hb=c5ac16de3ec4eb60580b1a4b6ea6fc0c63d75466;hpb=a7a78c7ac10e6bb6245dcdeab826f118ca2c60b0 diff --git a/src/day05.lisp b/src/day05.lisp new file mode 100644 index 0000000..ee60cd2 --- /dev/null +++ b/src/day05.lisp @@ -0,0 +1,40 @@ +(asdf:load-system :adventofcode2020) +(in-package #:adventofcode2020) + +(defun calc-seat (seat-spec) + (let ((row (string-right-trim "RL" seat-spec)) + (col (string-left-trim "FB" seat-spec)) + (convert-to-int + (lambda (str one) + (loop with len = (1- (length str)) + for c across str + for i downfrom len + summing (if (char= c one) + (ash 1 i) 0))))) + (mapcar convert-to-int (list row col) '(#\B #\R)))) + +(defun calc-seat-id (row-col) + (destructuring-bind (row col) row-col + (+ (* row 8) col))) + +(day 05 input + (let* ((lines (list-from input)) + (seat-ids (-<>> lines + (mapcar (compose #'calc-seat-id #'calc-seat)) + (sort <> #'>)))) + (part1 (first seat-ids)) + (part2 (loop for big in seat-ids + for small in (cdr seat-ids) + when (= small (- big 2)) + return (1+ small))))) + +(def-suite day05) +(in-suite day05) + +(test calc-seats + (is (equal + '((44 5) (70 7) (14 7) (102 4)) + (mapcar #'calc-seat '("FBFBBFFRLR" "BFFFBBFRRR" + "FFFBBBFRRR" "BBFFBBFRLL"))))) + +(run! 'day05)