Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(ns adventofcode.2018.day14)
(def day-input 846601)
(def day-input-vec [8 4 6 6 0 1])
(set! *unchecked-math* true)
(def initial-state {:workers #{0 1}
:table [3 7]})
(defn iteration [{:keys [workers table]}]
(let [scores (map (partial nth table)
recipe-sum (reduce + scores)
new-recipes (if (< recipe-sum 10) [recipe-sum]
[(quot recipe-sum 10) (rem recipe-sum 10)])
new-table (reduce conj table new-recipes)]
{:workers (mapv (fn [index] (mod (+ index (inc (nth table index)))
(count new-table)))
:table new-table}))
(defn compute-score-after [n]
(subvec (->> (iterate iteration initial-state)
(drop-while #(< (count (:table %))
(+ 10 n)))
n (+ n 10))
(defn contains-sequence-at-end? [table sequence]
(let [c1 (count table)
c2 (count sequence)]
(and (>= c1 c2)
(= sequence (subvec table (- c1 c2)))))
(defn solution2 [input]
(->> (nth (iterate iteration initial-state) (count input))
(iterate iteration)
(drop-while (comp not #(contains-sequence-at-end? (:table %) input)))
(drop-last (count input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment