Advertisement
Guest User

AOC-6

a guest
Dec 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.09 KB | None | 0 0
  1. (defun make-banks (&rest values)
  2.   (coerce values 'vector))
  3.  
  4. (defun find-max (banks &aux (index 0) imax vmax)
  5.   (map ()
  6.        (lambda (v)
  7.          (when (or (not vmax) (> v vmax))
  8.            (setf vmax v)
  9.            (setf imax index))
  10.          (incf index))
  11.        banks)
  12.   (values vmax imax))
  13.  
  14. (defun level-step (banks)
  15.   (prog1 banks
  16.     (multiple-value-bind (max pos) (find-max banks)
  17.       (let ((part (ceiling max (length banks))))
  18.         (setf (aref banks pos) 0)
  19.         (loop
  20.            repeat (length banks)
  21.            do
  22.              (let ((amount (min max part)))
  23.                (setf pos (mod (1+ pos) (length banks)))
  24.                (incf (aref banks pos) amount)
  25.                (decf max amount)))))))
  26.  
  27. (defun fixpoint (bank)
  28.   (let ((hash (make-hash-table :test #'equalp)))
  29.     (loop
  30.        for step from 0
  31.        for key = (copy-seq bank)
  32.        for past = (gethash key hash)
  33.        until past
  34.        do
  35.          (setf (gethash key hash) step)
  36.          (setf bank (level-step bank))
  37.        finally
  38.          (return
  39.            (values step (- step past))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement