projects
/
adventofcode2019.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
c57f6d9
)
Fix Intcode bug (uninitialized memory was nil)
author
Jack Kinsey
<j.jameskinsey@gmail.com>
Tue, 10 Dec 2019 00:50:47 +0000
(19:50 -0500)
committer
Jack Kinsey
<j.jameskinsey@gmail.com>
Tue, 10 Dec 2019 00:52:09 +0000
(19:52 -0500)
src/adventofcode2019/intcode.clj
patch
|
blob
|
blame
|
history
diff --git
a/src/adventofcode2019/intcode.clj
b/src/adventofcode2019/intcode.clj
index 252a6133350b88eb5836dc7c47279d2bd91c7ffd..dc542dd17e8240e49ca7a83efc950a092342ba38 100644
(file)
--- a/
src/adventofcode2019/intcode.clj
+++ b/
src/adventofcode2019/intcode.clj
@@
-9,10
+9,11
@@
op (vec (drop 3 str-code))
apply-flag (fn [flag arg]
(case flag
op (vec (drop 3 str-code))
apply-flag (fn [flag arg]
(case flag
- \0 (fn ([S] (get-in S [:memory arg]))
+ ;; ORs avoid returning nil
+ \0 (fn ([S] (or (get-in S [:memory arg]) 0))
([_ _] arg))
\1 (constantly arg)
([_ _] arg))
\1 (constantly arg)
- \2 (fn ([S] (
get-in S [:memory (+ arg (:relctr S))]
))
+ \2 (fn ([S] (
or (get-in S [:memory (+ arg (:relctr S))]) 0
))
([S _] (+ arg (:relctr S))))))
with-flags (fn [f]
(fn [S & args]
([S _] (+ arg (:relctr S))))))
with-flags (fn [f]
(fn [S & args]
@@
-21,20
+22,20
@@
(case op
[\0 \1] (fn [S a b c] ; ADD
(-> S
(case op
[\0 \1] (fn [S a b c] ; ADD
(-> S
- (assoc-in [:memory (c S
0
)] (+' (a S) (b S)))
+ (assoc-in [:memory (c S
true
)] (+' (a S) (b S)))
(update :ctr + 4)))
[\0 \2] (fn [S a b c] ; MULT
(-> S
(update :ctr + 4)))
[\0 \2] (fn [S a b c] ; MULT
(-> S
- (assoc-in [:memory (c S
0
)] (*' (a S) (b S)))
+ (assoc-in [:memory (c S
true
)] (*' (a S) (b S)))
(update :ctr + 4)))
[\0 \3] (fn [S a _ _] ; IN
(-> S
(update :ctr + 4)))
[\0 \3] (fn [S a _ _] ; IN
(-> S
- (assoc-in [:memory (a S
0
)] (first (:input S)))
+ (assoc-in [:memory (a S
true
)] (first (:input S)))
(update :input subvec 1)
(update :ctr + 2)))
[\0 \4] (fn [S a _ _] ; OUT
(-> S
(update :input subvec 1)
(update :ctr + 2)))
[\0 \4] (fn [S a _ _] ; OUT
(-> S
- (update :output conj (
get-in S [:memory (a S 0)]
))
+ (update :output conj (
or (get-in S [:memory (a S true)]) 0
))
(update :ctr + 2)))
[\0 \5] (fn [S a b _] ; BNEQ
(update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3))))
(update :ctr + 2)))
[\0 \5] (fn [S a b _] ; BNEQ
(update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3))))
@@
-42,11
+43,11
@@
(update S :ctr (if (= (a S) 0) (constantly (b S)) #(+ % 3))))
[\0 \7] (fn [S a b c] ; SLT
(-> S
(update S :ctr (if (= (a S) 0) (constantly (b S)) #(+ % 3))))
[\0 \7] (fn [S a b c] ; SLT
(-> S
- (assoc-in [:memory (c S
0
)] (if (< (a S) (b S)) 1 0))
+ (assoc-in [:memory (c S
true
)] (if (< (a S) (b S)) 1 0))
(update :ctr + 4)))
[\0 \8] (fn [S a b c] ; SEQ
(-> S
(update :ctr + 4)))
[\0 \8] (fn [S a b c] ; SEQ
(-> S
- (assoc-in [:memory (c S
0
)] (if (= (a S) (b S)) 1 0))
+ (assoc-in [:memory (c S
true
)] (if (= (a S) (b S)) 1 0))
(update :ctr + 4)))
[\0 \9] (fn [S a _ _] ; SREL
(-> S
(update :ctr + 4)))
[\0 \9] (fn [S a _ _] ; SREL
(-> S