advent-of-code-2024

My solutions to AoC 2024
Log | Files | Refs

commit 2b131a0c9cea5786a70d136aa843d2877776ef0e
parent 1fa9aa89ff2daf4cc4a43f917512963bf9a4ab6b
Author: Lukas Henkel <lh@entf.net>
Date:   Tue,  3 Dec 2024 06:27:59 +0100

Cleanup

Diffstat:
Msrc/day-3.lisp | 17+++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/day-3.lisp b/src/day-3.lisp @@ -3,21 +3,22 @@ (:export #:day-3)) (in-package #:aoc/day-3) +(defparameter *regex* "(mul\\((\\d+),(\\d+)\\))|((do|don't)\\(\\))") + (defun day-3 (input) (let ((input-string (read-stream-content-into-string input)) (mul-enabled t) (task-1 0) (task-2 0)) - (ppcre:do-scans (start end reg-starts reg-ends "(mul\\(\\d+,\\d+\\))|((do|don't)\\(\\))" input-string) + (ppcre:do-scans (start end reg-starts reg-ends *regex* input-string) (case (aref input-string start) (#\d (setf mul-enabled (char/= (aref input-string (+ start 2)) #\n))) (#\m - (multiple-value-bind (num-1 end) - (parse-integer input-string :start (+ start 4) :junk-allowed t) - (let* ((num-2 (parse-integer input-string :start (1+ end) :junk-allowed t)) - (result (* num-1 num-2))) - (incf task-1 result) - (when mul-enabled - (incf task-2 result))))))) + (let* ((num-1 (parse-integer input-string :start (aref reg-starts 1) :end (aref reg-ends 1))) + (num-2 (parse-integer input-string :start (aref reg-starts 2) :end (aref reg-ends 2))) + (result (* num-1 num-2))) + (incf task-1 result) + (when mul-enabled + (incf task-2 result)))))) (values task-1 task-2)))