1329a1895105

Day 6
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 06 Dec 2017 18:44:23 -0500
parents e0830b791f27
children bc917916aa2b
branches/tags (none)
files src/main.lisp

Changes

--- a/src/main.lisp	Tue Dec 05 21:00:49 2017 -0500
+++ b/src/main.lisp	Wed Dec 06 18:44:23 2017 -0500
@@ -130,3 +130,28 @@
     (incf (aref maze address)
           (if (>= offset 3) -1 1))
     (incf address offset)))
+
+
+(defun day-6/1+2 ()
+  (let* ((banks (coerce (read-file-of-numbers "data/2017/06") '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 cycles)
+        (summing 1 :into cycles)
+        (redistribute)
+        (for last-seen = (gethash banks seen))
+        (until last-seen)
+        (finally (return (values cycles (- cycles last-seen))))))))