-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8edd71f
commit 7b76b9b
Showing
3 changed files
with
44 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
(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))))) | ||
(defun parse-input (&optional (strings (aoc::read-problem-input 2023 08))) | ||
(destructuring-bind (instructions _ . network) strings | ||
(declare (ignore _)) | ||
(flet ((node (s) (mapcar #'as-keyword (cl-ppcre:all-matches-as-strings "\\w+" s)))) | ||
(cons (ncycle (coerce instructions 'list)) | ||
(mapcar #'node network))))) | ||
|
||
; (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 count-steps (start end? &optional (input (parse-input))) | ||
(destructuring-bind (instructions . network) input | ||
(recursively ((curr start) | ||
(instructions instructions) | ||
(steps 0)) | ||
(if (funcall end? curr) | ||
steps | ||
(destructuring-bind (left right) (assoc-value network curr) | ||
(ecase (car instructions) | ||
(#\L (recur left (cdr instructions) (1+ steps))) | ||
(#\R (recur right (cdr instructions) (1+ steps))))))))) | ||
|
||
(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 | ||
(define-solution (2023 08) (input parse-input) | ||
(values (count-steps :aaa [eq _ :zzz] input) | ||
(destructuring-bind (instructions . network) input | ||
(bnd* ((start-nodes (keep-if [string-ends-with-p "A" (mkstr _)] | ||
(alist-keys network) )) | ||
(end? [string-ends-with-p "Z" (mkstr _)])) | ||
(apply #'lcm (mapcar [count-steps _ end? input] start-nodes)))))) | ||
|
||
(define-test (2023 08) (21409 21165830176709)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
:mkstr | ||
:mulf | ||
:ncycle | ||
:recursively | ||
:repeat | ||
:string-ends-with-p | ||
:string-starts-with-p | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters