diff --git a/.gitignore b/.gitignore index 5818fae..943cd49 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ ## clojure stuff .clj-kondo .lsp +.cpcache diff --git a/2019/src/day01.clj b/2019/src/day01.clj new file mode 100644 index 0000000..0c4bfcb --- /dev/null +++ b/2019/src/day01.clj @@ -0,0 +1,138 @@ +(ns day01) + +(defn fuel-needed + "calculates fuel needed for given mass" + [mass] + (max (-> mass + (/ 3) + long + (- 2)) + 0)) + +(defn total-fuel-needed + "calculates total fuel needed accounting for weight of gas as well" + ([mass] + (total-fuel-needed mass 0)) + ([mass acc] + (let [f (fuel-needed mass)] + (if (= f 0) + acc + (recur f (+ acc f)))))) + +(comment + (require '[clojure.string :as str]) + + ; part 1 + (->> input + str/split-lines + (map parse-long) + (map fuel-needed) + (reduce +)) + + ; part 2 + (->> input + str/split-lines + (map parse-long) + (map total-fuel-needed) + (reduce +)) + + (def input "83453 +89672 +81336 +74923 +71474 +117060 +55483 +116329 +123515 +99383 +80314 +108221 +128335 +72860 +139235 +127843 +140120 +63561 +68854 +109062 +146211 +59096 +123085 +105763 +127657 +142212 +111007 +100166 +63641 +59010 +108575 +93619 +144095 +74561 +95059 +145318 +81404 +96567 +91799 +92987 +107137 +87678 +126842 +85594 +116330 +104714 +128117 +132641 +75602 +90747 +69038 +67322 +146147 +147535 +83266 +85908 +124634 +51681 +104430 +56202 +68631 +69970 +116985 +140878 +125357 +126229 +66379 +103213 +108210 +73855 +130992 +113363 +82298 +111468 +110751 +52272 +103661 +122262 +114363 +80881 +65183 +125291 +100119 +56995 +101634 +55467 +136284 +107433 +95647 +71462 +133265 +104554 +62499 +61347 +68675 +123501 +113954 +135798 +80825 +128235")) diff --git a/2019/src/day02.clj b/2019/src/day02.clj new file mode 100644 index 0000000..c0299bc --- /dev/null +++ b/2019/src/day02.clj @@ -0,0 +1,52 @@ +(ns day02) + +(defn compute + "takes in a tape & runs the program + returns the state of tape when program halts" + ([tape] + (compute 0 tape)) + ([pos tape] + (let [op (tape pos)] + (if (= op 99) + tape + (let [arg1-slot (tape (inc pos)) + arg1-value (tape arg1-slot) + arg2-slot (tape (+ pos 2)) + arg2-value (tape arg2-slot) + return-slot (tape (+ pos 3))] + (recur (+ pos 4) + (assoc tape return-slot + ((condp = op + 1 + + 2 *) + arg1-value arg2-value)))))))) + +(comment + ;; part 1 + (-> input + (assoc 1 12) + (assoc 2 2) + compute + (get 0)) + + ;; part 2 + (->> (for [x (range 80) + y (range 80)] + (let [t (-> input + (assoc 1 x) + (assoc 2 y) + compute)] + {:noun (t 1) + :verb (t 2) + :val (t 0)})) + (filter #(= (:val %) 19690720)) + (map (fn [{:keys [noun verb]}] + (+ (* 100 noun) verb))) + first) + +;; set up input + (let [raw-input "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,10,19,23,1,6,23,27,1,5,27,31,1,10,31,35,2,10,35,39,1,39,5,43,2,43,6,47,2,9,47,51,1,51,5,55,1,5,55,59,2,10,59,63,1,5,63,67,1,67,10,71,2,6,71,75,2,6,75,79,1,5,79,83,2,6,83,87,2,13,87,91,1,91,6,95,2,13,95,99,1,99,5,103,2,103,10,107,1,9,107,111,1,111,6,115,1,115,2,119,1,119,10,0,99,2,14,0,0"] + (require '[clojure.string :as str]) + (def input (->> (str/split raw-input #",") + (mapv parse-long))) + input))