Skip to content

Instantly share code, notes, and snippets.

@oliverholworthy
Created December 11, 2017 10:58
Show Gist options
  • Save oliverholworthy/c84848f85e6847e0dc1f9b3f23c88e55 to your computer and use it in GitHub Desktop.
Save oliverholworthy/c84848f85e6847e0dc1f9b3f23c88e55 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css">
<script>
window.klipse_settings = {
selector: '.language-klipse'
};
</script>
</head>
<body>
<pre>
<code class="language-klipse">
(defn apply-length [{:keys [skip-size xs position]} length]
(let [xs-length (take length (drop position (cycle xs)))]
{:xs (replace (zipmap xs-length (reverse xs-length)) xs)
:position (mod (+ position length skip-size) (count xs))
:skip-size (inc skip-size)}))
(defn ascii [c] (.charCodeAt c 0))
(defn hex [n] (.substr (+ "00" (.toString n 16)) -2))
(defn sparse-hash [s]
(let [lengths (concat (map ascii s) [17 31 73 47 23])]
(:xs (nth (iterate #(reduce apply-length % lengths)
{:skip-size 0 :position 0 :xs (range 256)})
64))))
(defn dense-hash [xs] (map #(apply bit-xor %) (partition 16 xs)))
(defn hex-hash [xs] (apply str (map hex xs)))
(def knot-hash (comp hex-hash dense-hash sparse-hash))
(knot-hash "AoC 2017")
</code>
</pre>
<script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment