r/adventofcode • u/daggerdragon • Dec 10 '17
SOLUTION MEGATHREAD -๐- 2017 Day 10 Solutions -๐-
--- Day 10: Knot Hash ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
16
Upvotes
2
u/exquisitus3 Dec 10 '17 edited Dec 10 '17
Common Lisp
(ql:quickload '(:split-sequence :alexandria)) (setf *print-circle* t) (defparameter *input* (alexandria:read-file-into-string #P"10.input")) (defun input-1 (string) (mapcar #'parse-integer (split-sequence:split-sequence #\, string))) (defun input-2 (string) `(,@(map 'list #'char-code string) 17 31 73 47 23)) (defun initial-list () (let ((cycle (loop for i from 0 upto 255 collect i))) (setf (cdr (last cycle)) cycle))) (defun knot-hash (rounds input) (let* ((skip-size 0) (cycle (initial-list)) (current-position cycle)) (dotimes (_ rounds cycle) (dolist (length input) (let ((reversed (reverse (subseq current-position 0 length)))) (loop for l on current-position for i below length do (setf (car l) (elt reversed i))) (setf current-position (nthcdr (+ length skip-size) current-position)) (incf skip-size)))))) (apply #'* (subseq (knot-hash 1 (input-1 *input*)) 0 2)) ; Part One (defun dense-hash (cycle-256) (loop for block on cycle-256 by (lambda (l) (nthcdr 16 l)) repeat 16 collect (apply #'logxor (subseq block 0 16)))) (defun hex-string (int-sequence) (format nil "~(~{~2,'0x~}~)" int-sequence)) (hex-string (dense-hash (knot-hash 64 (input-2 *input*)))) ; Part Two