From 2dea3570215ff7dc82745b34cef92eacf26b6c85 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 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 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..707315d --- /dev/null +++ b/2024/3/main.lisp @@ -0,0 +1,35 @@ +(asdf:load-system :cl-ppcre) + +(defun solve-1 (line) + (let ((result 0)) + (ppcre:do-register-groups (a b) ("mul\\(([0-9]+),([0-9]+)\\)" line) + (incf result (* (parse-integer a) (parse-integer b)))) + result)) + +(defun solve-2 (line) + (let ((result 0) + (enabled t)) + (ppcre:do-scans (start + end + starts + ends + "(mul\\(([0-9]+),([0-9]+)\\))|(do\\(\\))|(don't\\(\\))" + line) + (let ((str (subseq line start end))) + (cond + ((string= str "do()") + (setf enabled t)) + ((string= str "don't()") + (setf enabled nil)) + (enabled + (let ((a (subseq line (aref starts 1) (aref ends 1))) + (b (subseq line (aref starts 2) (aref ends 2)))) + (incf result (* (parse-integer a) (parse-integer b)))))))) + result)) + +(defun main () + (let ((input (uiop:read-file-string "data.txt"))) + (format t + "1: ~A~%2: ~A~%" + (solve-1 input) + (solve-2 input))))