Fix day11pt1 and add correct day11pt2
[adventofcode2019.git] / src / adventofcode2019 / intcode.clj
index 4914fcdda3d71411b8036fc06a5ed5b3c598f7b3..84a96254126ed52d7dcbb53c4e8c808be06aced9 100644 (file)
         [\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))))