-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns day14 | ||
(:require [clj-commons.digest :refer [md5]])) | ||
|
||
(defn hashes [it n] (pmap #(nth (iterate md5 (str it %)) n) (range))) | ||
|
||
(defn same-chars [n h] | ||
(reduce (fn [a e] (if (apply = e) (reduced (first e)) a)) nil (partition n 1 h))) | ||
|
||
(defn key64 [it n] | ||
(loop [i 0, [h & hr] (hashes it n) , hm {}, hs []] | ||
(cond | ||
(> (count hs) 63) (nth (sort hs) 63) | ||
:else (let [k3 (same-chars 3 h), k5 (when k3 (same-chars 5 h)) | ||
idx (when-let [c (hm k5)] (filter #(<= i (+ 1000 %)) c))] | ||
;; (when (= (mod i 1000) 0) (prn (/ i 1000) (count hs))) | ||
(recur (inc i) hr (cond-> hm k3 (update k3 conj i)) (if (seq idx) (into hs idx) hs)))))) | ||
|
||
;; part 1 takes around half a sec, but part 2 runs for more than a minute -- weak performance of `md5` | ||
|
||
(defn -main [day] | ||
(let [it "ihaygndm", solve (partial key64 it)] | ||
{:part1 (solve 1) :part2 (solve 2017)})) | ||
|
||
|
||
(comment | ||
(= 22728 (key64 "abc" 1)) ; not testing part2, takes too long | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters