Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns aoc2019.day2
- (:require [clojure.string :as s]))
- (def ops {1 +
- 2 *})
- (defn read-input []
- (-> (slurp "resources/day2.txt")
- (s/trim-newline)
- (s/split #",")
- (->> (map #(Integer/parseInt %)))))
- (defn calc-result [prog op]
- (let [cmd (ops (first op))
- arg1 (nth prog (nth op 1))
- arg2 (nth prog (nth op 2))
- result-pos (last op)]
- (assoc prog result-pos (cmd arg1 arg2))))
- (defn run [prog pos]
- (let [op (take 4 (drop pos prog))]
- (if (= 99 (first op))
- (first prog)
- (run (calc-result prog op) (+ pos 4)))))
- (defn init [input n v]
- (-> input
- vec
- (assoc 1 n)
- (assoc 2 v)))
- (def target 19690720)
- (defn find-args [input]
- (loop [n 0 v 0]
- (let [res (run (init input n v) 0)]
- (if (= res target)
- (+ (* n 100) v)
- (if (= 99 v)
- (recur (inc n) 0)
- (recur n (inc v)))))))
- (defn solve-p1 []
- (let [input (read-input)
- prog (init input 12 2)]
- (run prog 0)))
- (defn solve-p2 []
- (let [input (read-input)]
- (find-args input)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement