[\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))))
([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))))