Skip to content

Commit

Permalink
Day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
caseneuve committed Jan 27, 2023
1 parent a4952ea commit 62b0ba4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Clojure/day14.clj
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
)
3 changes: 2 additions & 1 deletion bb.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:paths ["input" "Clojure"]
:deps {org.clojure/math.combinatorics {:mvn/version "0.1.6"}}
:deps {org.clojure/math.combinatorics {:mvn/version "0.1.6"}
org.clj-commons/digest {:mvn/version "1.4.100"}}
:tasks
{:requires ([babashka.curl :as curl]
[babashka.fs :as fs]
Expand Down

0 comments on commit 62b0ba4

Please sign in to comment.