From: Jack Kinsey Date: Tue, 10 Dec 2019 00:29:06 +0000 (-0500) Subject: Fix Intcode bug X-Git-Url: http://git.jkinsey.net/?p=adventofcode2019.git;a=commitdiff_plain;h=cce5143765e0bf17f2d844c54401109983576ede Fix Intcode bug Turns out relative mode parameters can be used in write-to-memory positions, so the wrapper function needed to be changed to account for that. It's a little ugly, but it's the best I've got. --- diff --git a/src/adventofcode2019/intcode.clj b/src/adventofcode2019/intcode.clj index 72fe618..252a613 100644 --- a/src/adventofcode2019/intcode.clj +++ b/src/adventofcode2019/intcode.clj @@ -9,9 +9,11 @@ op (vec (drop 3 str-code)) apply-flag (fn [flag arg] (case flag - \0 (fn ([] arg) ([S] (get-in S [:memory arg]))) + \0 (fn ([S] (get-in S [:memory arg])) + ([_ _] arg)) \1 (constantly arg) - \2 #(get-in % [:memory (+' arg (:relctr %))]))) + \2 (fn ([S] (get-in S [:memory (+ arg (:relctr S))])) + ([S _] (+ arg (:relctr S)))))) with-flags (fn [f] (fn [S & args] (apply f S (map apply-flag flags args))))] @@ -19,20 +21,20 @@ (case op [\0 \1] (fn [S a b c] ; ADD (-> S - (assoc-in [:memory (c)] (+' (a S) (b S))) + (assoc-in [:memory (c S 0)] (+' (a S) (b S))) (update :ctr + 4))) [\0 \2] (fn [S a b c] ; MULT (-> S - (assoc-in [:memory (c)] (*' (a S) (b S))) + (assoc-in [:memory (c S 0)] (*' (a S) (b S))) (update :ctr + 4))) [\0 \3] (fn [S a _ _] ; IN (-> S - (assoc-in [:memory (a)] (first (:input S))) + (assoc-in [:memory (a S 0)] (first (:input S))) (update :input subvec 1) (update :ctr + 2))) [\0 \4] (fn [S a _ _] ; OUT (-> S - (update :output conj (get-in S [:memory (a)])) + (update :output conj (get-in S [:memory (a S 0)])) (update :ctr + 2))) [\0 \5] (fn [S a b _] ; BNEQ (update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3)))) @@ -40,11 +42,11 @@ (update S :ctr (if (= (a S) 0) (constantly (b S)) #(+ % 3)))) [\0 \7] (fn [S a b c] ; SLT (-> S - (assoc-in [:memory (c)] (if (< (a S) (b S)) 1 0)) + (assoc-in [:memory (c S 0)] (if (< (a S) (b S)) 1 0)) (update :ctr + 4))) [\0 \8] (fn [S a b c] ; SEQ (-> S - (assoc-in [:memory (c)] (if (= (a S) (b S)) 1 0)) + (assoc-in [:memory (c S 0)] (if (= (a S) (b S)) 1 0)) (update :ctr + 4))) [\0 \9] (fn [S a _ _] ; SREL (-> S