commit 7d1af509cb7409b51302378d1e40a7e9416a38fc parent 4cb0b879d1a96dd9a5afd45be2a80fcf631faeb4 Author: Lukas Henkel <lh@entf.net> Date: Sat, 2 Dec 2023 07:12:55 +0100 Don't use property lists for sets Diffstat:
M | src/day-2.lisp | | | 38 | ++++++++++++++++++++------------------ |
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/day-2.lisp b/src/day-2.lisp @@ -5,7 +5,9 @@ (defun parse-sets (line start) (loop with sets = nil - with current-set = nil + with red = 0 + with green = 0 + with blue = 0 for pos from start below (length line) do (multiple-value-bind (n end) (parse-integer line :start pos :junk-allowed t) @@ -15,12 +17,15 @@ (char= #\; char))) line :start pos)) - (setf (getf current-set (make-keyword (upcase (subseq line (1+ pos) end)))) n - pos (or end (1- (length line))))) + (ecase (aref line (1+ pos)) + (#\r (setf red n)) + (#\g (setf green n)) + (#\b (setf blue n))) + (setf pos (or end (1- (length line))))) when (char= (aref line pos) #\;) - do (push current-set sets) - and do (setf current-set nil) - finally (return (cons current-set sets)))) + do (push (list red green blue) sets) + and do (setf red 0 green 0 blue 0) + finally (return (cons (list red green blue) sets)))) (defun parse-game-line (line) (let* ((start-game-id 5) @@ -28,23 +33,20 @@ (list (parse-integer line :start start-game-id :end end-game-id) (parse-sets line (1+ end-game-id))))) +(defparameter *minimum-cubes* (list 12 13 14)) + (defun game-possible-p (sets) (loop for set in sets - for red = (getf set :red) - for green = (getf set :green) - for blue = (getf set :blue) - always (and (or (null red) - (<= red 12)) - (or (null green) - (<= green 13)) - (or (null blue) - (<= blue 14))))) + always (loop for color-1 in set + for color-2 in *minimum-cubes* + always (<= color-1 color-2)))) (defun game-power (sets) (apply #'* - (mapcar (lambda (color) - (apply #'max (cons 0 (remove nil (mapcar (rcurry #'getf color) sets))))) - (list :red :green :blue)))) + (apply #'mapcar + (cons (lambda (&rest values) + (apply #'max values)) + sets)))) (defun day-2 (input) (loop for line = (read-line input nil)