src/2017/days/day-16.lisp @ b18a8ad31735
2016/05
author |
Steve Losh <steve@stevelosh.com> |
date |
Sat, 07 Dec 2019 18:41:25 -0500 |
parents |
2078ac8647c6 |
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)))))