]> localhost Git - adventofcode2019.git/commitdiff
Add untested day 2 pt 1 solution
authorJack Kinsey <kinsey_john@bah.com>
Mon, 2 Dec 2019 15:49:59 +0000 (10:49 -0500)
committerJack Kinsey <kinsey_john@bah.com>
Mon, 2 Dec 2019 15:49:59 +0000 (10:49 -0500)
day02/intcode.clj [new file with mode: 0644]

diff --git a/day02/intcode.clj b/day02/intcode.clj
new file mode 100644 (file)
index 0000000..f6e5666
--- /dev/null
@@ -0,0 +1,32 @@
+(ns aoc.core
+  [:require [clojure.string :as str]])
+
+(def get-list-from-file
+  #(str/split #"," (slurp %)))
+
+(def parse-int
+  #(Integer/parseInt %))
+
+(def opcodes {1 +, 2 *})
+(defn perform-operation [[opcode fst snd thd] program]
+  (let [opcode-fn (opcodes opcode)
+        fst-val (program fst)
+        snd-val (program snd)]
+    (assoc program thd (opcode-fn fst-val snd-val))))
+
+(defn intcode [program counter]
+  (loop [program program
+         counter counter]
+    (let [opcode (program counter)]
+      (if (= opcode 99)
+        program
+        (recur (perform-operation (map program (take 4 (iterate inc counter)))
+                                  program)
+               (+ counter 4))))))
+
+(defn -main []
+  (let [input (mapv parse-int (get-list-from-file "input"))
+        fixed-input (assoc input 1 12 2 1)] 
+    (println (first (intcode fixed-input)))))
+
+(-main)