SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn addr [program idx]
  2.   (nth program idx))
  3.  
  4. (defn value [program idx]
  5.   (nth program (addr program idx)))
  6.  
  7. (defn next-op [idx]
  8.   (+ 4 idx))
  9.  
  10. (defn eval-op [program op idx]
  11.   (assoc program
  12.          (addr program (+ 3 idx))
  13.          (op (value program (inc idx))
  14.              (value program (+ 2 idx)))))
  15.  
  16. (defn execute [program]
  17.   (loop [program program
  18.          i 0]
  19.     (let [opcode (get program i)]
  20.       (case opcode
  21.         1 (recur (eval-op program + i)
  22.                  (next-op i))
  23.         2 (recur (eval-op program * i)
  24.                  (next-op i))
  25.         99 program))))
  26.  
  27. (defn restore [program]
  28.   (assoc program
  29.          1 12
  30.          2 2))
  31.  
  32. (defn parse-input [content]
  33.   (->> (clojure.string/split (clojure.string/trim-newline content) #",")
  34.        (map #(Integer/valueOf %))
  35.        vec))
  36.  
  37. (-> (slurp "/tmp/input")
  38.     parse-input
  39.     restore
  40.     execute
  41.     first) ;; => 5534943
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top