-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday10.lisp
More file actions
65 lines (55 loc) · 1.93 KB
/
day10.lisp
File metadata and controls
65 lines (55 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(defpackage :aoc/2017/10 #.cl-user::*aoc-use*
(:export
:knot-hash))
(in-package :aoc/2017/10)
(defvar *size*)
(setf *size* 256)
(defvar *additional-lengths*)
(setf *additional-lengths* (list 17 31 73 47 23))
(defun parse-line-of-integers (s)
(mapcar #'parse-integer (split-sequence:split-sequence #\, s)))
(defun parse-ascii-codes (s)
(mapcar #'char-int (coerce s 'list)))
(defun scramble (hash lengths current skip)
(flet ((twist (hash start end)
(loop
:for i = start :then (1+ i)
:for j = end :then (1- j)
:for temp = (aref hash (mod i *size*))
:while (<= i j)
:do (setf (aref hash (mod i *size*)) (aref hash (mod j *size*))
(aref hash (mod j *size*)) temp))))
(loop
:for len :in lengths
:for i = current
:for j = (+ i (1- len))
:do (twist hash i j)
:do (incf current (+ len skip))
:do (incf skip)
:finally (return (values current skip)))))
(defun dense-hash (hash)
(loop
:for i = 0 :then (+ i 16)
:while (array-in-bounds-p hash i)
:for slice = (subseq hash i (+ i 16))
:collecting (reduce #'logxor slice)))
(defun knot-hash-init (&aux (hash (make-array *size*)))
(dotimes (n *size* hash)
(setf (aref hash n) n)))
(defun knot-hash (data)
(let ((hash (knot-hash-init))
(lengths (append (parse-ascii-codes data) *additional-lengths*))
(current 0)
(skip 0))
(dotimes (n 64 (hexadecimal-string (dense-hash hash)))
(multiple-value-bind (c s) (scramble hash lengths current skip)
(setf current c
skip s)))))
(define-solution (2017 10) (data first)
(values
(let ((hash (knot-hash-init))
(lengths (append (parse-line-of-integers data) *additional-lengths*)))
(scramble hash lengths 0 0)
(* (aref hash 0) (aref hash 1)))
(knot-hash data)))
(define-test (2017 10) (23874 "e1a65bfb5a5ce396025fab5528c25a87"))