From d2892f1a3b351b641ffc82eede73354ff7a8930d Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Mon, 2 Dec 2024 21:50:19 -0800 Subject: [PATCH] 2024 day 3 --- 2024/3/main.lisp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2024/3/main.lisp diff --git a/2024/3/main.lisp b/2024/3/main.lisp new file mode 100644 index 0000000..6a2549d --- /dev/null +++ b/2024/3/main.lisp @@ -0,0 +1,28 @@ +(asdf:load-system :cl-ppcre) + +(defun solve-1 (line) + (let ((result 0)) + (ppcre:do-register-groups (a b) + ("mul\\(([0-9]+),([0-9]+)\\)" line result) + (incf result (* (parse-integer a) (parse-integer b)))))) + +(defun solve-2 (line) + (let ((result 0) + (enabled t)) + (ppcre:do-register-groups (mul a b dop dontp) + ("(mul\\(([0-9]+),([0-9]+)\\))|(do\\(\\))|(don't\\(\\))" line result) + (declare (ignore mul)) + (cond + (dop + (setf enabled t)) + (dontp + (setf enabled nil)) + (enabled + (incf result (* (parse-integer a) (parse-integer b)))))))) + +(defun main () + (let ((input (uiop:read-file-string "data.txt"))) + (format t + "1: ~A~%2: ~A~%" + (solve-1 input) + (solve-2 input))))