From: Jack Kinsey Date: Thu, 5 Dec 2019 19:47:54 +0000 (-0500) Subject: Add untested day5pt1 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=8d5bfececf759369f09f8ddc77ee85f13e248c40;p=adventofcode2019.git Add untested day5pt1 --- diff --git a/src/adventofcode2019/core.clj b/src/adventofcode2019/core.clj index fb1a320..2cb3822 100644 --- a/src/adventofcode2019/core.clj +++ b/src/adventofcode2019/core.clj @@ -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 index 0000000..0b2420d --- /dev/null +++ b/src/adventofcode2019/day05.clj @@ -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)))