Created
December 14, 2018 09:06
-
-
Save Heliosmaster/4971270311f00f3f8af1ccb7c0d29adf to your computer and use it in GitHub Desktop.
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
(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) | |
workers) | |
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))) | |
workers) | |
:table new-table})) | |
(defn compute-score-after [n] | |
(subvec (->> (iterate iteration initial-state) | |
(drop-while #(< (count (:table %)) | |
(+ 10 n))) | |
first | |
:table) | |
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))) | |
first | |
:table | |
(drop-last (count input)) | |
(count)) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment