commit b1251b8a6682777e96178b3927705f9883028715 parent 751ebe7c2c2e21ce1e7fed21ff71bab45f22ba9b Author: Lukas Henkel <lh@entf.net> Date: Sat, 2 Dec 2023 10:45:06 +0100 Don't search for end position, hardcode color name length Diffstat:
M | src/day-2.lisp | | | 27 | +++++++++++++-------------- |
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/src/day-2.lisp b/src/day-2.lisp @@ -9,20 +9,19 @@ with red = 0 with green = 0 with blue = 0 - for pos from start below (length line) + with length = (length line) + for pos from start do (multiple-value-bind (n end) (parse-integer line :start pos :junk-allowed t) (setf pos end) - (setf end (position-if (lambda (char) - (or (char= #\, char) - (char= #\; char))) - line - :start pos)) (ecase (aref line (1+ pos)) - (#\r (setf red n)) - (#\g (setf green n)) - (#\b (setf blue n))) - (setf pos (or end (1- (length line))))) + (#\r (setf red n + pos (+ pos 4))) + (#\g (setf green n + pos (+ pos 6))) + (#\b (setf blue n + pos (+ pos 5))))) + while (< pos length) when (char= (aref line pos) #\;) do (push (list red green blue) sets) and do (setf red 0 green 0 blue 0) @@ -30,10 +29,10 @@ (declaim (ftype (function (simple-string) list) parse-game-line)) (defun parse-game-line (line) - (let* ((start-game-id 5) - (end-game-id (position #\: line))) - (list (parse-integer line :start start-game-id :end end-game-id) - (parse-sets line (1+ end-game-id))))) + (multiple-value-bind (game-id colon-pos) + (parse-integer line :start 5 :junk-allowed t) + (list game-id + (parse-sets line (1+ colon-pos))))) (defparameter *minimum-cubes* (list 12 13 14))