0b2420d0a4ed34c508b177da6e7c47222dee256f
[adventofcode2019.git] / src / adventofcode2019 / day05.clj
1 (ns adventofcode2019.day02
2 [:require [adventofcode2019.lib :refer :all]
3 [clojure.string :as str]])
4
5 (def operations {1 [#(assoc %4 %3 (+ %1 %2)) 3]
6 2 [#(assoc %4 %3 (* %1 %2)) 3]
7 3 [#(do (println (%2 %1)) %2) 1]
8 4 [#(assoc %2 %1 (parse-int (read-line))) 1]})
9 (def decode-op (memoize (fn [opcode]
10 (let [str-code (format "%05d" opcode)
11 [f3 f2 f1] (map #(= \1 %) (take 3 str-code))
12 op (parse-int (str/join (drop 3 str-code)))
13 [operation arg-ct] (operations op)]
14 [(case arg-ct
15 1 #(operation ((if f1 identity %1) %2) %1)
16 3 #(operation ((if f1 identity %1) %2)
17 ((if f2 identity %1) %3)
18 ((if f3 identity %1) %4) %1)
19 nil) arg-ct]))))
20
21 (defn perform-operation [program counter]
22 (let [opcode (program counter)
23 [operation arg-ct] (decode-op opcode)
24 args (->> (iterate inc counter)
25 (rest)
26 (take arg-ct)
27 (map program))]
28 [(apply operation program args)
29 (+ counter arg-ct 1)]))
30
31 (defn intcode [program]
32 (loop [[program counter] [program 0]]
33 (let [opcode (program counter)]
34 (if (= opcode 99)
35 program
36 (recur (perform-operation program counter))))))
37
38 (defn day02 []
39 (let [input (mapv parse-int (get-list-from-file (input-file) #","))]
40 (part1 "see below")
41 (intcode input)
42 #_(part2)))