Skip to content

Instantly share code, notes, and snippets.

@pavloo
Created December 24, 2017 21:31
Show Gist options
  • Save pavloo/cfaecde383300c5fe22b0efd7124ccfd to your computer and use it in GitHub Desktop.
Save pavloo/cfaecde383300c5fe22b0efd7124ccfd to your computer and use it in GitHub Desktop.
;;; -*- lexical-binding: t -*-
(defun dowrap (list beg_i l lambd)
(let (i)
(dotimes (j l)
(setq i (+ beg_i j))
(setq i (% i (length list)))
(apply lambd (list i j))
)
)
)
(dowrap '(1 2 3 4) 2 2 (lambda (i j) (print (format "i %d j %d" i j)))
)
(defun sublist-wrap (list beg_i l)
(let (res)
(dowrap
list
beg_i
l
(lambda (i j)
(push (nth i list) res)
)
)
res
)
)
(defun reverse-wrap-list (list beg_i l)
(let ((rev-sub (sublist-wrap list beg_i l)))
(dowrap
list
beg_i
l
(lambda (i j)
(setf (nth i list) (nth j rev-sub))
)
)
)
list
)
(sublist-wrap '(1 2 3 4) 2 3) ;; 1 4 3
(sublist-wrap '(1 2 3 4) 0 2) ;; 2 1
(sublist-wrap '(0 1 2 3) 0 3)
(reverse-wrap-list '(1 2 3 4 5) 0 3) ;; 3 2 1 4 5
(reverse-wrap-list '(1 2 3 4 5 6) 4 4) ;; 6 5 3 4 2 1
(defun knot-hash-round (list input i off)
(let ()
(dolist (el input)
(reverse-wrap-list list i el)
(setq i (+ i (+ el off)))
(setq i (% i (length list)))
(setq off (1+ off))
)
(print (format "Part 1: %d" (* (nth 0 list) (nth 1 list))))
(list i off)
)
)
(knot-hash-round '(0 1 2 3 4) '(3 4 1 5) 0 0) ;; 12
(defun generate-input ()
(let (res)
(dotimes (i 256 (reverse res))
(push i res)
)
)
)
(defun to-hex (n)
(substring (format "0%x" n) -2)
)
(defun knot-hash (input)
(let (round_res (rounds 64) (list (generate-input)) (input (append input '(17 31 73 47 23))) (i 0) (off 0))
(dotimes (_ rounds)
(setq round_res (knot-hash-round list input i off))
(setq i (nth 0 round_res))
(setq off (nth 1 round_res))
)
(let (res (acc 0) val)
(dotimes (i (length list) (string-join (reverse res) ""))
(setq val (nth i list))
(setq acc (logxor acc val))
(when (= (% (1+ i) 16) 0) (progn
(push (to-hex acc) res)
(setq acc 0)
)
)
)
)
)
)
(knot-hash (convert-to-ascii "192,69,168,160,78,1,166,28,0,83,198,2,254,255,41,12"))
(knot-hash-round (generate-input) '(192 69 168 160 78 1 166 28 0 83 198 2 254 255 41 12) 0 0)
(defun convert-to-ascii (str)
(mapcar (lambda (ch) (aref ch 0)) (split-string str "" t))
)
(knot-hash (convert-to-ascii "")) ;; a2582a3a0e66e6e86e3812dcb672a272
(knot-hash (convert-to-ascii "AoC 2017")) ;; 33efeb34ea91902bb2f59c9920caa6cd
(knot-hash (convert-to-ascii "1,2,3")) ;; 3efbe78a8d82f29979031a4aa0b16a9d
(knot-hash (convert-to-ascii "1,2,4")) ;; 63960835bcdc130f0b66d7ff4f6a5a8e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment