Skip to content

Instantly share code, notes, and snippets.

@death
Created December 10, 2017 19:17
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/6d8271d50db60893706b097e5570322b to your computer and use it in GitHub Desktop.
Save death/6d8271d50db60893706b097e5570322b to your computer and use it in GitHub Desktop.
;;;; +----------------------------------------------------------------+
;;;; | Advent of Code 2017 |
;;;; +----------------------------------------------------------------+
(defpackage #:snippets/aoc2017/day10
(:use #:cl)
(:import-from #:alexandria #:iota)
(:import-from #:constantia #:outs #:out)
(:export
#:day10))
(in-package #:snippets/aoc2017/day10)
(defun circ-aref (vector k)
(aref vector (mod k (length vector))))
(defun (setf circ-aref) (new-value vector k)
(setf (aref vector (mod k (length vector))) new-value))
(defun rev (knots pos len)
(dotimes (i (floor len 2))
(rotatef (circ-aref knots (+ pos i))
(circ-aref knots (+ pos (- i) len -1))))
knots)
(defun make-circ (n)
(coerce (iota n) 'vector))
(defun puzzle->lengths (puzzle)
(if (listp puzzle)
puzzle
(append (map 'list #'char-code puzzle)
'(17 31 73 47 23))))
(defun multiply (knots)
(* (aref knots 0)
(aref knots 1)))
(defun compress (knots)
(outs
(:dc
(:e (dotimes (i 16)
(loop with s = 0
repeat 16
for j upfrom (* i 16)
do (setf s (logxor s (aref knots j)))
finally (out (:d s :base 16 :width 2 :pad-char #\0))))))))
(defun day10 (puzzle &key (list-length 256)
(rounds 64)
(finalize #'compress))
(let ((lengths (puzzle->lengths puzzle))
(knots (make-circ list-length))
(pos 0)
(skip 0))
(loop repeat rounds
do (dolist (len lengths)
(rev knots pos len)
(incf pos len)
(incf pos skip)
(incf skip)))
(funcall finalize knots)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment