summary refs log tree commit diff
path: root/2024/d03.lisp
diff options
context:
space:
mode:
Diffstat (limited to '2024/d03.lisp')
-rw-r--r--2024/d03.lisp37
1 files changed, 37 insertions, 0 deletions
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