Created
December 11, 2024 13:14
-
-
Save DeLaGuardo/830af058cc038e7688317d96e1bcfac1 to your computer and use it in GitHub Desktop.
aoc24_d11
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
(require '[clojure.string :as string]) | |
(def blink | |
(memoize | |
(fn [times n] | |
(if (zero? times) | |
1 | |
(let [l (count n) | |
l-half (bit-shift-right l 1) | |
even? (= (+ l-half l-half) l)] | |
(cond | |
(= n "0") | |
(blink (dec times) "1") | |
even? | |
(let [sl (subs n 0 l-half) | |
sr (string/replace-first (subs n l-half) #"^0+" "") | |
sr (if (zero? (count sr)) "0" sr)] | |
(+ (blink (dec times) sl) | |
(blink (dec times) sr))) | |
:else | |
(blink (dec times) (str (*' (parse-long n) 2024))))))))) | |
(defn solve [times numbers] | |
(transduce | |
(comp (map str) | |
(map #(blink times %))) | |
+ | |
numbers)) | |
(comment | |
(time | |
(dotimes [_ 100000] | |
(solve 25 '(125 17)))) | |
(time | |
(dotimes [_ 100000] | |
(solve 75 '(64599 31 674832 2659361 1 0 8867 321))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment