From aac046c5af8f598b758ac3ba860c6cb9212dc86e Mon Sep 17 00:00:00 2001 From: Matteo Landi Date: Fri, 8 Dec 2023 06:40:42 +0100 Subject: [PATCH] Add 2023/08 --- src/2023/day08.lisp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/2023/day08.lisp diff --git a/src/2023/day08.lisp b/src/2023/day08.lisp new file mode 100644 index 0000000..2a6ee38 --- /dev/null +++ b/src/2023/day08.lisp @@ -0,0 +1,35 @@ +(defpackage :aoc/2023/08 #.cl-user::*aoc-use*) +(in-package :aoc/2023/08) + +(defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2023/day08.txt"))) + (destructuring-bind (instructions ignore . nodes) strings + (flet ((node (s) + (mapcar #'as-keyword (cl-ppcre:all-matches-as-strings "\\w+" s)))) + (cons (coerce instructions 'list) + (mapcar #'node nodes))))) + +; (loop with (inst . nodes) = (parse-input) +; for step from 0 +; for node = (assoc :aaa nodes) then (assoc next nodes) +; for in in (ncycle inst) for next = (if (eq in #\L) (second node) (third node)) +; when (eq (car node) :zzz) return step) +; => 21409 + +(defun node-name-ends-with-char? (ch node) + (char= (char (reverse (mkstr (first node))) 0) ch)) +(node-name-ends-with-char? #\A '(:aaz)) + +(defun find-part2 (start &optional (strings (uiop:read-file-lines #P"src/2023/day08.txt"))) + (loop with (inst . nodes) = (parse-input) + for step from 0 + for node = (assoc start nodes) then (assoc next nodes) + for in in (ncycle inst) for next = (if (eq in #\L) (second node) (third node)) + when (node-name-ends-with-char? #\Z node) return step + never (not node))) + +; (apply +; #'lcm +; (looping +; (dolist (start (remove-if-not [node-name-ends-with-char? #\A _] (cdr (parse-input)))) +; (collect! (find-part2 (car start)))))) +; => 21165830176709