ee60cd2e80b036429559e35dc0b616a1d4f79253
[adventofcode2020.git] / src / day05.lisp
1 (asdf:load-system :adventofcode2020)
2 (in-package #:adventofcode2020)
3
4 (defun calc-seat (seat-spec)
5 (let ((row (string-right-trim "RL" seat-spec))
6 (col (string-left-trim "FB" seat-spec))
7 (convert-to-int
8 (lambda (str one)
9 (loop with len = (1- (length str))
10 for c across str
11 for i downfrom len
12 summing (if (char= c one)
13 (ash 1 i) 0)))))
14 (mapcar convert-to-int (list row col) '(#\B #\R))))
15
16 (defun calc-seat-id (row-col)
17 (destructuring-bind (row col) row-col
18 (+ (* row 8) col)))
19
20 (day 05 input
21 (let* ((lines (list-from input))
22 (seat-ids (-<>> lines
23 (mapcar (compose #'calc-seat-id #'calc-seat))
24 (sort <> #'>))))
25 (part1 (first seat-ids))
26 (part2 (loop for big in seat-ids
27 for small in (cdr seat-ids)
28 when (= small (- big 2))
29 return (1+ small)))))
30
31 (def-suite day05)
32 (in-suite day05)
33
34 (test calc-seats
35 (is (equal
36 '((44 5) (70 7) (14 7) (102 4))
37 (mapcar #'calc-seat '("FBFBBFFRLR" "BFFFBBFRRR"
38 "FFFBBBFRRR" "BBFFBBFRLL")))))
39
40 (run! 'day05)