Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn addr [program idx]
- (nth program idx))
- (defn value [program idx]
- (nth program (addr program idx)))
- (defn next-op [idx]
- (+ 4 idx))
- (defn eval-op [program op idx]
- (assoc program
- (addr program (+ 3 idx))
- (op (value program (inc idx))
- (value program (+ 2 idx)))))
- (defn execute [program]
- (loop [program program
- i 0]
- (let [opcode (get program i)]
- (case opcode
- 1 (recur (eval-op program + i)
- (next-op i))
- 2 (recur (eval-op program * i)
- (next-op i))
- 99 program))))
- (defn restore [program]
- (assoc program
- 1 12
- 2 2))
- (defn parse-input [content]
- (->> (clojure.string/split (clojure.string/trim-newline content) #",")
- (map #(Integer/valueOf %))
- vec))
- (-> (slurp "/tmp/input")
- parse-input
- restore
- execute
- first) ;; => 5534943
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement