# HG changeset patch # User Steve Losh # Date 1575220422 18000 # Node ID 7de124d6ef6f8c8a7bf6bda7a15e921649e632e0 # Parent 5b5c61ad8d2bb6b12aa9ee5e4b683fe456bbca94 More porting from old years diff -r 5b5c61ad8d2b -r 7de124d6ef6f src/2017/day-06.lisp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/2017/day-06.lisp Sun Dec 01 12:13:42 2019 -0500 @@ -0,0 +1,29 @@ +(defpackage :advent/2017/06 #.cl-user::*advent-use*) +(in-package :advent/2017/06) + + +(define-problem (2017 6) (data read-all) (6681 2392) + (let ((banks (coerce data 'vector)) + (seen (make-hash-table :test 'equalp))) + (labels ((bank-to-redistribute () + (iterate (for blocks :in-vector banks :with-index bank) + (finding bank :maximizing blocks))) + (redistribute () + (iterate + (with bank = (bank-to-redistribute)) + (with blocks-to-redistribute = (aref banks bank)) + (initially (setf (aref banks bank) 0)) + (repeat blocks-to-redistribute) + (for b :modulo (length banks) :from (1+ bank)) + (incf (aref banks b)))) + (mark-seen (banks cycles) + (setf (gethash (copy-seq banks) seen) cycles))) + (iterate + (mark-seen banks cycle) + (counting t :into cycle) + (redistribute) + (for last-seen = (gethash banks seen)) + (until last-seen) + (finally (return (values cycle (- cycle last-seen)))))))) + +