aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-18 21:56:57 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-18 21:56:57 +0100
commit8cf7836157162ed977fe5fefa43ef3cb9e5c6fab (patch)
tree54026a937088de3a5e12e54a8c14ee5f86de17f9
parentf554040de2feca3ae22b8a9d4fb7b8fa8f6e5a81 (diff)
downloadscratch-8cf7836157162ed977fe5fefa43ef3cb9e5c6fab.tar.gz
scratch-8cf7836157162ed977fe5fefa43ef3cb9e5c6fab.tar.bz2
scratch-8cf7836157162ed977fe5fefa43ef3cb9e5c6fab.zip
unify solution with part1
-rw-r--r--AoC2022/16/solver.lisp55
1 files changed, 13 insertions, 42 deletions
diff --git a/AoC2022/16/solver.lisp b/AoC2022/16/solver.lisp
index f7c35b6..bdd8fc3 100644
--- a/AoC2022/16/solver.lisp
+++ b/AoC2022/16/solver.lisp
@@ -87,34 +87,6 @@
(= 1651 (path-release '(AA DD BB JJ HH EE CC) (worthwhile-graph (data "eg-in")) 30)))
(fiveam:is
(= 560 (path-release '(AA DD) (worthwhile-graph (data "eg-in")) 30))))
-;;
-;; part 1
-(let* ((open '(aa))
- (action-graph (worthwhile-graph (data "input")))
- (start-time 30))
- ;; (path-release)
-
- ;; (loop for k being the hash-keys in open using (hash-value v)
- ;; do (format t "~a=>~a~%" k v))
- (labels ((traverse (graph node open time-left current-flow)
- (let ((next (next-options (gethash node graph) open time-left)))
- (if (null next)
- (reverse open)
- (arrows:->
- (lambda (next-node)
- (destructuring-bind (name . time-there) next-node
- (let ((flow (car (gethash name graph)))
- (time-left (- time-left time-there 1)))
- (traverse graph name (cons name open)
- time-left
- (+ current-flow
- (* flow time-left))))))
- (mapcar next)
- (sort #'>= :key (lambda (path) (path-release path graph start-time)))
- (car))))))
- (traverse action-graph 'aa open start-time 0)))
-
-;; part 2
(defun path-release2 (paths graph start-time)
(reduce #'+ paths
@@ -134,13 +106,12 @@
;; '(AA RA XK YH NM YP XS)
;; (worthwhile-graph (data "input")))
-(defun part2 (filename)
- (let* ((action-graph (worthwhile-graph (data filename)))
- (start-time 26)
- (actor (list `((aa) ,start-time 0) `((aa) ,start-time 0))))
+(defun accumulated-flow (actor-paths)
+ (reduce #'+ actor-paths :key #'caddr))
- ;; (loop for k being the hash-keys in open using (hash-value v)
- ;; do (format t "~a=>~a~%" k v))
+(defun solver (filename start-time actors)
+ (let* ((action-graph (worthwhile-graph (data filename)))
+ (actor (loop repeat actors collect `((aa) ,start-time 0))))
(labels ((traverse (graph actor open)
(if (loop for ac in actor
:always (zerop (cadr ac)))
@@ -170,20 +141,20 @@
(cons name open)))))
(mapcar next)
- (sort #'>= :key (lambda (actor-paths)
- ;; (path-release2 actor-paths graph start-time)
- (reduce #'+ actor-paths :key #'caddr)
- ))
+ (sort #'>= :key #'accumulated-flow)
(car))))))))
;; (princ)
(traverse action-graph actor '(aa)))))
-(fiveam:test solution2
+(fiveam:test solutions
+ (fiveam:is
+ (= 1651 (accumulated-flow (solver "eg-in" 30 1))))
+ (fiveam:is
+ (= 1754 (accumulated-flow (solver "input" 30 1))))
(fiveam:is
- (= 1707 (reduce #'+ (solver "eg-in" 26 2) :key #'caddr)))
+ (= 1707 (accumulated-flow (solver "eg-in" 26 2))))
(fiveam:is
- (= 2474 (reduce #'+ (part2 "input") :key #'caddr)))
- )
+ (= 2474 (accumulated-flow (solver "input" 26 2)))))
(fiveam:run-all-tests)
;; (require :sb-sprof)