master
4903000 4 years ago
parent cf566277f8
commit 96a81894e2

@ -176,3 +176,39 @@
(let ((data (read-4 file))) (let ((data (read-4 file)))
(time (progn (print (%solve-4 data *passport-rules-broken*)) (time (progn (print (%solve-4 data *passport-rules-broken*))
(print (%solve-4 data *passport-rules*)))))) (print (%solve-4 data *passport-rules*))))))
(defun binarize (string bits)
(parse-integer (substitute
#\1 (elt bits 1)
(substitute
#\0 (elt bits 0) string))
:radix 2))
(defun read-5 (file)
(with-open-file (stream file)
(loop for line = (read-line stream nil)
while line
collect (map 'list #'binarize (list (subseq line 0 7) (subseq line 7)) '("FB" "LR")))))
(defun boarding-ids (data)
(map 'list (lambda (seat) (+ (* 8 (first seat)) (second seat))) data))
(defun solve-5-1 (data)
(first (sort (boarding-ids data) #'>)))
(defvar p1-answer)
(defun report-significant-difference (x y)
(if (not (= (- x y) 1))
(setf p1-answer (1- x)))
y)
(defun solve-5-2 (data)
(reduce #'report-significant-difference (sort (boarding-ids data) #'>)))
(defun solve-5 (file)
(declare (special p1-answer)) (let ((data (read-5 file))
(p1-answer nil))
(time (progn (print (solve-5-1 (copy-seq data)))
(solve-5-2 data)
(print p1-answer)))))

Loading…
Cancel
Save