Add untested day5pt1
authorJack Kinsey <kinsey_john@bah.com>
Thu, 5 Dec 2019 19:47:54 +0000 (14:47 -0500)
committerJack Kinsey <kinsey_john@bah.com>
Thu, 5 Dec 2019 19:47:54 +0000 (14:47 -0500)
src/adventofcode2019/core.clj
src/adventofcode2019/day05.clj [new file with mode: 0644]

index fb1a3209fe111f09f982bd70f190278a3ac88745..2cb3822a572c908fe850effb3df68edf24b69f26 100644 (file)
@@ -1,6 +1,6 @@
 (ns adventofcode2019.core
     [:require (adventofcode2019 day01 day02 day03
-                                day04)])
+                                day04 day05)])
 
 (defn -main 
   ([]
diff --git a/src/adventofcode2019/day05.clj b/src/adventofcode2019/day05.clj
new file mode 100644 (file)
index 0000000..0b2420d
--- /dev/null
@@ -0,0 +1,42 @@
+(ns adventofcode2019.day02
+    [:require [adventofcode2019.lib :refer :all]
+              [clojure.string :as str]])
+
+(def operations {1 [#(assoc %4 %3 (+ %1 %2)) 3] 
+                 2 [#(assoc %4 %3 (* %1 %2)) 3] 
+                 3 [#(do (println (%2 %1)) %2) 1]
+                 4 [#(assoc %2 %1 (parse-int (read-line))) 1]})
+(def decode-op (memoize (fn [opcode] 
+  (let [str-code (format "%05d" opcode)
+        [f3 f2 f1] (map #(= \1 %) (take 3 str-code))
+        op (parse-int (str/join (drop 3 str-code)))
+        [operation arg-ct] (operations op)]
+    [(case arg-ct
+       1 #(operation ((if f1 identity %1) %2) %1)
+       3 #(operation ((if f1 identity %1) %2) 
+                     ((if f2 identity %1) %3) 
+                     ((if f3 identity %1) %4) %1)
+       nil) arg-ct]))))
+
+(defn perform-operation [program counter]
+   (let [opcode (program counter)
+         [operation arg-ct] (decode-op opcode)
+         args (->> (iterate inc counter) 
+                   (rest) 
+                   (take arg-ct) 
+                   (map program))]
+     [(apply operation program args)
+      (+ counter arg-ct 1)]))
+
+(defn intcode [program]
+  (loop [[program counter] [program 0]]
+    (let [opcode (program counter)]
+      (if (= opcode 99)
+        program
+        (recur (perform-operation program counter))))))
+
+(defn day02 []
+  (let [input (mapv parse-int (get-list-from-file (input-file) #","))]
+    (part1 "see below") 
+    (intcode input)
+    #_(part2)))