Finish day2, add project structure
[adventofcode2019.git] / src / adventofcode2019 / day02.clj
1 (ns adventofcode2019.day02
2 [:require [adventofcode2019.lib :refer :all]
3 [clojure.string :as str]
4 [clojure.math.combinatorics :as combo]])
5
6 (def opcodes {1 +, 2 *})
7 (defn perform-operation [[opcode fst snd thd] program]
8 (let [opcode-fn (opcodes opcode)
9 fst-val (program fst)
10 snd-val (program snd)]
11 (assoc program thd (opcode-fn fst-val snd-val))))
12
13 (defn intcode [program]
14 (loop [program program
15 counter 0]
16 (let [opcode (program counter)]
17 (if (= opcode 99)
18 program
19 (recur (perform-operation (map program (take 4 (iterate inc counter)))
20 program)
21 (+ counter 4))))))
22
23 (defn check-inputs [input noun verb]
24 (let [fixed-input (assoc input 1 noun 2 verb)]
25 (first (intcode fixed-input))))
26
27 (defn day02 []
28 (let [input (mapv parse-int (get-list-from-file (input-file) #","))
29 [noun verb] (first (filter (fn [[noun verb]] (= (check-inputs input noun verb) 19690720))
30 (combo/cartesian-product (range 100) (range 100))))]
31 (println (check-inputs input 12 2)) ;; part 1
32 (println (+ (* 100 noun) verb)))) ;; part 2