X-Git-Url: http://git.jkinsey.net/?p=adventofcode2019.git;a=blobdiff_plain;f=src%2Fadventofcode2019%2Fintcode.clj;fp=src%2Fadventofcode2019%2Fintcode.clj;h=84a96254126ed52d7dcbb53c4e8c808be06aced9;hp=4914fcdda3d71411b8036fc06a5ed5b3c598f7b3;hb=9205379113b838bcad0f87d9dfa770fc7c3d0b83;hpb=b8da8334cb639cbbd04c7a96b1311cb644fdf705 diff --git a/src/adventofcode2019/intcode.clj b/src/adventofcode2019/intcode.clj index 4914fcd..84a9625 100644 --- a/src/adventofcode2019/intcode.clj +++ b/src/adventofcode2019/intcode.clj @@ -31,11 +31,11 @@ [\0 \3] (fn [S a _ _] ; IN (-> S (assoc-in [:memory (a S true)] (first (:input S))) - (update :input subvec 1) + (update :input rest) (update :ctr + 2))) [\0 \4] (fn [S a _ _] ; OUT (-> S - (update :output conj (or (get-in S [:memory (a S true)]) 0)) + (update :output conj (a S)) (update :ctr + 2))) [\0 \5] (fn [S a b _] ; BNEQ (update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3)))) @@ -68,15 +68,15 @@ ([program settings] (merge (build-state program) settings))) +(defn intcode [{:as state :keys [memory output]}] + (cond ; quit if :exit, step and return state if :step, else loop + (get state :exit) {:memory memory :output output :exit true} + (get state :step) (perform-operation state) + :else (recur (perform-operation state)))) + (defn intcode-until [pred state] (as-> (assoc state :step true) it (iterate intcode it) - (drop-while #(or (not (:exit %)) (pred %)) it) + (drop-while #(not (or (:exit %) (pred %))) it) (first it) (dissoc it :step))) - -(defn intcode [{:as state :keys [memory output]}] - (cond ; quit if :exit, step and return state if :step, else loop - (get state :exit) {:memory memory :output output :exit true} - (get state :step) (perform-operation state) - :else (recur (perform-operation state))))