src/2017/days/day-16.lisp @ 2078ac8647c6

2017 16, 17, 18p1
author Steve Losh <steve@stevelosh.com>
date Fri, 06 Dec 2019 20:58:57 -0500
parents (none)
children 32db20d16280
(defpackage :advent/2017/16 #.cl-user::*advent-use*)
(in-package :advent/2017/16)

(defparameter *initial* "abcdefghijklmnop")

(defun shift (array &aux (tmp (aref array (1- (length array)))))
  (dotimes (i (length array))
    (rotatef tmp (aref array i))))

(defun spin (dancers n)
  (do-repeat n (shift dancers)))

(defun exchange (dancers i j)
  (rotatef (aref dancers i)
           (aref dancers j)))

(defun partner (dancers a b)
  (rotatef (aref dancers (position a dancers))
           (aref dancers (position b dancers))))

(defun dance (dancers steps &key (repeat 1))
  (do-repeat repeat
    (iterate
      (for step :in steps)
      (ecase (char step 0)
        (#\s (spin dancers (parse-integer step :start 1)))
        (#\x (destructuring-bind (i j) (read-numbers-from-string step)
               (exchange dancers i j)))
        (#\p (partner dancers (char step 1) (char step 3))))))
  dancers)

(defun find-cycle (steps)
  (iterate
    (with dancers = (copy-seq *initial*))
    (dance dancers steps)
    (counting t)
    (until (string= dancers *initial*))))

(define-problem (2017 16) (data read-comma-separated-values)
    ("ionlbkfeajgdmphc" "fdnphiegakolcmjb")
  (values
    (dance (copy-seq *initial*) data)
    (dance (copy-seq *initial*) data :repeat (mod 1000000000 (find-cycle data)))))