Skip to content

Instantly share code, notes, and snippets.

@death
Created December 6, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save death/02e716211827683c48a9c580348fd5a5 to your computer and use it in GitHub Desktop.
Save death/02e716211827683c48a9c580348fd5a5 to your computer and use it in GitHub Desktop.
;;;; +----------------------------------------------------------------+
;;;; | Advent of Code 2017 |
;;;; +----------------------------------------------------------------+
(defpackage #:snippets/aoc2017/day6
(:use #:cl)
(:export
#:day6))
(in-package #:snippets/aoc2017/day6)
(defun blocks (banks k)
(aref banks k))
(defun (setf blocks) (new-value banks k)
(setf (aref banks k) new-value))
(defun choose (banks)
(position (reduce #'max banks) banks))
(defun next (k banks)
(mod (1+ k) (length banks)))
(defun redistribute (banks)
(let* ((top (choose banks))
(n (blocks banks top)))
(setf (blocks banks top) 0)
(do ((i (next top banks)
(next i banks)))
((zerop n))
(incf (blocks banks i))
(decf n))
banks))
(defun day6 (puzzle)
(let ((seen (make-hash-table :test 'equalp))
(banks (copy-seq puzzle))
(cycle 0))
(loop until (gethash banks seen)
do (setf (gethash (copy-seq banks) seen) cycle)
(setf banks (redistribute banks))
(incf cycle))
(values (hash-table-count seen)
(- cycle (gethash banks seen)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment