From 6970fcacf00860cf6b7ab04b61c58dc2935d53c4 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 19 Dec 2022 05:07:07 +0100 Subject: denest more --- AoC2022/16/solver.lisp | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/AoC2022/16/solver.lisp b/AoC2022/16/solver.lisp index 9d07505..158704d 100644 --- a/AoC2022/16/solver.lisp +++ b/AoC2022/16/solver.lisp @@ -105,34 +105,36 @@ (and (zerop (logand next-node-name open)) (< (1+ time-there) time-left)))) - (defun traverse (graph actor open) (if (loop for ac in actor :always (zerop (cadr ac))) (mapcar (lambda (a) (cons (reverse (car a)) (cdr a))) actor) - (destructuring-bind (path time-left previous-flow) (car actor) - (let* ((current-options (assoc (car path) graph :test #'=)) - (flow-released (caddr current-options)) - (current-flow (+ previous-flow (* flow-released time-left))) - (next (remove-if-not (lambda (node) - (appropriate node open time-left)) - (cdddr current-options)))) - (if (null next) - (traverse graph - (append (cdr actor) - (list (list path 0 current-flow))) - open) - - (arrows:-> - (loop for (name . time-there) in next - collect - (let ((time-left (- time-left time-there 1))) - (traverse graph - (append (cdr actor) (list (list (cons name path) time-left - current-flow))) - (logior name open)))) - (sort #'>= :key #'accumulated-flow) - (car))))))) + (advance graph actor open))) + +(defun advance (graph actor open) + (destructuring-bind (path time-left previous-flow) (car actor) + (let* ((current-options (assoc (car path) graph :test #'=)) + (flow-released (caddr current-options)) + (current-flow (+ previous-flow (* flow-released time-left))) + (next (remove-if-not (lambda (node) + (appropriate node open time-left)) + (cdddr current-options)))) + (if (null next) + (traverse graph + (append (cdr actor) + (list (list path 0 current-flow))) + open) + + (arrows:-> + (loop for (name . time-there) in next + collect + (let ((time-left (- time-left time-there 1))) + (traverse graph + (append (cdr actor) (list (list (cons name path) time-left + current-flow))) + (logior name open)))) + (sort #'>= :key #'accumulated-flow) + (car)))))) (defun solver (filename start-time actors) (let* ((action-graph (worthwhile-graph (data filename))) -- cgit v1.2.3