SHARE
TWEET

Untitled

a guest Dec 5th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns alarm.core
  2.   (:require [clojure.string :refer [split trim-newline]]))
  3.  
  4. (defn interpret
  5.   ([mem] (interpret mem 0))
  6.   ([mem pos]
  7.    (let [op (mem pos)
  8.          x (get mem (inc pos))
  9.          y (get mem (+ 2 pos))
  10.          xv (get mem x)
  11.          yv (get mem y)
  12.          t (get mem (+ 3 pos))
  13.          write #(update mem t (constantly %))]
  14.      (case op
  15.        1 (recur (write (+ xv yv)) (+ 4 pos))
  16.        2 (recur (write (* xv yv)) (+ 4 pos))
  17.        (first mem)))))
  18.  
  19. (defn set-mem [mem noun verb]
  20.   (-> mem
  21.       (update 1 (constantly noun))
  22.       (update 2 (constantly verb))))
  23.  
  24. (defn find-res [num mem]
  25.   (for [noun (range 100)
  26.         verb (range 100)
  27.         :when (= (interpret
  28.                   (set-mem mem noun verb))
  29.                  num)]
  30.     (+ (* 100 noun) verb)))
  31.                  
  32. (defn read-input! [filename]
  33.   (as-> (slurp filename) l
  34.     (trim-newline l)
  35.     (split l #",")
  36.     (mapv #(Integer/parseInt %) l)))                    
  37.  
  38. (defn -main []
  39.   (let [mem (read-input! "input.txt")            
  40.         one (interpret mem)
  41.         two (first (find-res 19690720 mem))]
  42.     (println one)
  43.     (println two)))
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