Update day 05 with shorter solution
[adventofcode2020.git] / src / day05.lisp
index ee60cd2e80b036429559e35dc0b616a1d4f79253..214553a3abd6f794efe74d11137882189660c27d 100644 (file)
@@ -2,25 +2,16 @@
 (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)))
+  (loop with len = (1- (length seat-spec))
+        for c across seat-spec
+        for i downfrom len
+        summing (if (or (char= c #\R) (char= c #\B))
+                    (ash 1 i) 0)))
 
 (day 05 input
   (let* ((lines (list-from input))
          (seat-ids (-<>> lines 
-                     (mapcar (compose #'calc-seat-id #'calc-seat)
+                     (mapcar #'calc-seat
                      (sort <> #'>))))
     (part1 (first seat-ids))
     (part2 (loop for big in seat-ids
@@ -33,7 +24,7 @@
 
 (test calc-seats
   (is (equal
-        '((44 5) (70 7) (14 7) (102 4))
+        '(357 567 119 820)
         (mapcar #'calc-seat '("FBFBBFFRLR" "BFFFBBFRRR" 
                               "FFFBBBFRRR" "BBFFBBFRLL")))))