(in-package :aoc/2024) (defparameter *input/d3* (asdf:system-relative-pathname :advent "2024/d03.input")) (print *input/d3*) (defun readfile (filename) (let ((lines '())) (with-open-file (stream filename :direction :input) (loop for line = (read-line stream nil nil) while line do (push line lines))) (nreverse lines))) (defun replace-with-prefix-expr (match) (ppcre:regex-replace "mul\\(([0-9]+),([0-9]+)\\)" match "(* \\1 \\2)")) (print (apply '+ (mapcar 'eval (mapcar #'read-from-string (mapcar #'replace-with-prefix-expr (ppcre:all-matches-as-strings "mul\\(([0-9]+),([0-9]+)\\)" (format nil "~{~a~}" (readfile *input/d3*)))))))) ; 1795571322 (print (apply '+ (mapcar 'eval (mapcar #'read-from-string (mapcar #'replace-with-prefix-expr (ppcre:all-matches-as-strings "mul\\(([0-9]+),([0-9]+)\\)" (ppcre:regex-replace-all "don't\\(\\).*?(do\\(\\)|$)" (format nil "~{~a~}" (readfile *input/d3*)) ""))))))) ; 103811193