(ns adventofcode2019.day02 [:require [adventofcode2019.lib :refer :all] [clojure.string :as str] [clojure.math.combinatorics :as combo]]) (def opcodes {1 +, 2 *}) (defn perform-operation [[opcode fst snd thd] program] (let [opcode-fn (opcodes opcode) fst-val (program fst) snd-val (program snd)] (assoc program thd (opcode-fn fst-val snd-val)))) (defn intcode [program] (loop [program program counter 0] (let [opcode (program counter)] (if (= opcode 99) program (recur (perform-operation (map program (take 4 (iterate inc counter))) program) (+ counter 4)))))) (defn check-inputs [input noun verb] (let [fixed-input (assoc input 1 noun 2 verb)] (first (intcode fixed-input)))) (defn day02 [] (let [input (mapv parse-int (get-list-from-file (input-file) #",")) [noun verb] (first (filter (fn [[noun verb]] (= (check-inputs input noun verb) 19690720)) (combo/cartesian-product (range 100) (range 100))))] (println (check-inputs input 12 2)) ;; part 1 (println (+ (* 100 noun) verb)))) ;; part 2