From 9ce100f7a4177cd06bbb136a4d26db1ba58f4552 Mon Sep 17 00:00:00 2001 From: Emile Date: Tue, 3 Dec 2024 21:01:33 +0100 Subject: 2024 day three part 1: converting all the given mul() values to their infix notation and summing up the list part 2: removing everything between the don't() and do() as well as between don't() and the end of the line, the reset is the same as in the first part --- 2024/d03.lisp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2024/d03.lisp (limited to '2024/d03.lisp') diff --git a/2024/d03.lisp b/2024/d03.lisp new file mode 100644 index 0000000..c194f97 --- /dev/null +++ b/2024/d03.lisp @@ -0,0 +1,37 @@ +(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 -- cgit 1.4.1