From 96a81894e20cc836fc0fecc5f51cb3043377afc3 Mon Sep 17 00:00:00 2001 From: 4903000 <4903000@chiru.no> Date: Sat, 5 Dec 2020 22:48:27 +0000 Subject: [PATCH] Day 5 --- aoc20.lisp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/aoc20.lisp b/aoc20.lisp index 30cb708..f68d5f3 100644 --- a/aoc20.lisp +++ b/aoc20.lisp @@ -176,3 +176,39 @@ (let ((data (read-4 file))) (time (progn (print (%solve-4 data *passport-rules-broken*)) (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)))))