Add day 05
[adventofcode2020.git] / src / day05.lisp
diff --git a/src/day05.lisp b/src/day05.lisp
new file mode 100644 (file)
index 0000000..ee60cd2
--- /dev/null
@@ -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)