Skip to content

Commit

Permalink
Add 2023/08
Browse files Browse the repository at this point in the history
  • Loading branch information
iamFIREcracker committed Dec 8, 2023
1 parent 70c9c95 commit aac046c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/2023/day08.lisp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aac046c

Please sign in to comment.