From d1bbb525d19c4ff491795ed6365d7203d49a7f6d Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Sat, 5 Dec 2020 19:02:43 -0500 Subject: [PATCH] Update day 05 with shorter solution Thanks, SSCDCS. --- src/day05.lisp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) 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"))))) -- 2.38.5