b21ffd2588dd456722d9e17cadfc0910204d498c
[adventofcode2019.git] / day02 / intcode.clj
1 (ns aoc.core
2 [:require [clojure.string :as str]])
3
4 (def get-list-from-file
5 #(str/split #"," (slurp %)))
6
7 (def parse-int
8 #(Integer/parseInt %))
9
10 (def opcodes {1 +, 2 *})
11 (defn perform-operation [[opcode fst snd thd] program]
12 (let [opcode-fn (opcodes opcode)
13 fst-val (program fst)
14 snd-val (program snd)]
15 (assoc program thd (opcode-fn fst-val snd-val))))
16
17 (defn intcode [program]
18 (loop [program program
19 counter 0]
20 (let [opcode (program counter)]
21 (if (= opcode 99)
22 program
23 (recur (perform-operation (map program (take 4 (iterate inc counter)))
24 program)
25 (+ counter 4))))))
26
27 (defn -main []
28 (let [input (mapv parse-int (get-list-from-file "input"))
29 fixed-input (assoc input 1 12 2 1)]
30 (println (first (intcode fixed-input)))))
31
32 (-main)