Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(def input [1,12,2,3, 1,1,2,3, 1,3,4,3, 1,5,0,3, 2,13,1,19, 1,5,19,23, 2,10,23,27, 1,27,5,31, 2,9,31,35, 1,35,5,39, 2,6,39,43, 1,43,5,47, 2,47,10,51, 2,51,6,55, 1,5,55,59, 2,10,59,63, 1,63,6,67, 2,67,6,71, 1,71,5,75, 1,13,75,79, 1,6,79,83, 2,83,13,87, 1,87,6,91, 1,10,91,95, 1,95,9,99, 2,99,13,103, 1,103,6,107, 2,107,6,111, 1,111,2,115, 1,115,13,0, 99,2,0,14, 0])
(defn add [m p1 p2 p3]
(let [a (get m p1)
b (get m p2)]
(assoc m p3 (+ a b))))
(defn mul [m p1 p2 p3]
(let [a (get m p1)
b (get m p2)]
(assoc m p3 (* a b))))
(defn calculate! [memory]
(loop [cp 0]
(let [[opcode p1 p2 p3] (->> memory deref (drop cp))]
(case opcode
1 (do (swap! memory add p1 p2 p3)
(recur (+ cp 4)))
2 (do (swap! memory mul p1 p2 p3)
(recur (+ cp 4)))
99 (get @memory 0)
:else :error))))
;; part 1
(calculate! (atom input))
;; part 2
(loop [[[noun verb] & rest] (for [x (range 100)
y (range 100)]
[x y])]
(let [memory (atom input)
_ (swap! memory assoc 1 noun 2 verb)
result (calculate! memory)]
(cond
(= result 19690720) (+ (* 100 noun) verb)
(empty? rest) :not-found
:else (recur rest))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.