From: Jack Kinsey Date: Sun, 6 Dec 2020 00:02:43 +0000 (-0500) Subject: Update day 05 with shorter solution X-Git-Url: http://git.jkinsey.net/?p=adventofcode2020.git;a=commitdiff_plain;h=d1bbb525d19c4ff491795ed6365d7203d49a7f6d;ds=sidebyside Update day 05 with shorter solution Thanks, SSCDCS. --- diff --git a/src/day05.lisp b/src/day05.lisp index ee60cd2..214553a 100644 --- a/src/day05.lisp +++ b/src/day05.lisp @@ -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")))))