summary refs log tree commit diff
path: root/2024/d03.lisp
diff options
context:
space:
mode:
authorEmile <git@emile.space>2024-12-03 21:01:33 +0100
committerEmile <git@emile.space>2024-12-03 21:01:33 +0100
commit9ce100f7a4177cd06bbb136a4d26db1ba58f4552 (patch)
tree20ed6a53350f139090c97b52d7d001d168c6be2a /2024/d03.lisp
parent7b72f794891bb885d1dc4944be6e97758c4c2a90 (diff)
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
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