From cca08f5d8b235974166ebf040bf8426bf1ad786e Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Mon, 2 Dec 2019 20:43:00 -0500 Subject: [PATCH] Finish day2, add project structure --- .gitignore | 12 +++++++ day02/intcode.clj | 32 ------------------- project.clj | 10 ++++++ day01/input => resources/day01 | 0 resources/day02 | 1 + src/adventofcode2019/core.clj | 9 ++++++ .../adventofcode2019/day01.clj | 17 +++------- src/adventofcode2019/day02.clj | 32 +++++++++++++++++++ src/adventofcode2019/lib.clj | 16 ++++++++++ src/adventofcode2019/template.clj | 8 +++++ test/adventofcode2019/core_test.clj | 7 ++++ 11 files changed, 100 insertions(+), 44 deletions(-) create mode 100644 .gitignore delete mode 100644 day02/intcode.clj create mode 100644 project.clj rename day01/input => resources/day01 (100%) create mode 100644 resources/day02 create mode 100644 src/adventofcode2019/core.clj rename day01/fuel.clj => src/adventofcode2019/day01.clj (60%) create mode 100644 src/adventofcode2019/day02.clj create mode 100644 src/adventofcode2019/lib.clj create mode 100644 src/adventofcode2019/template.clj create mode 100644 test/adventofcode2019/core_test.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d18f225 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +/target +/classes +/checkouts +profiles.clj +pom.xml +pom.xml.asc +*.jar +*.class +/.lein-* +/.nrepl-port +.hgignore +.hg/ diff --git a/day02/intcode.clj b/day02/intcode.clj deleted file mode 100644 index b21ffd2..0000000 --- a/day02/intcode.clj +++ /dev/null @@ -1,32 +0,0 @@ -(ns aoc.core - [:require [clojure.string :as str]]) - -(def get-list-from-file - #(str/split #"," (slurp %))) - -(def parse-int - #(Integer/parseInt %)) - -(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 -main [] - (let [input (mapv parse-int (get-list-from-file "input")) - fixed-input (assoc input 1 12 2 1)] - (println (first (intcode fixed-input))))) - -(-main) diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..fe88a58 --- /dev/null +++ b/project.clj @@ -0,0 +1,10 @@ +(defproject adventofcode2019 "0.1.0-SNAPSHOT" + :description "Advent of Code 2019" + :url "http://example.com/FIXME" +;;:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" +;; :url "https://www.eclipse.org/legal/epl-2.0/"} + :dependencies [[org.clojure/clojure "1.10.0"] + [org.clojure/math.combinatorics "0.1.6"]] + :main ^:skip-aot adventofcode2019.core + :target-path "target/%s" + :profiles {:uberjar {:aot :all}}) diff --git a/day01/input b/resources/day01 similarity index 100% rename from day01/input rename to resources/day01 diff --git a/resources/day02 b/resources/day02 new file mode 100644 index 0000000..99d3840 --- /dev/null +++ b/resources/day02 @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,1,23,13,27,2,6,27,31,1,5,31,35,2,10,35,39,1,6,39,43,1,13,43,47,2,47,6,51,1,51,5,55,1,55,6,59,2,59,10,63,1,63,6,67,2,67,10,71,1,71,9,75,2,75,10,79,1,79,5,83,2,10,83,87,1,87,6,91,2,9,91,95,1,95,5,99,1,5,99,103,1,103,10,107,1,9,107,111,1,6,111,115,1,115,5,119,1,10,119,123,2,6,123,127,2,127,6,131,1,131,2,135,1,10,135,0,99,2,0,14,0 diff --git a/src/adventofcode2019/core.clj b/src/adventofcode2019/core.clj new file mode 100644 index 0000000..0e47a72 --- /dev/null +++ b/src/adventofcode2019/core.clj @@ -0,0 +1,9 @@ +(ns adventofcode2019.core + [:require (adventofcode2019 day01 day02)]) + +(defn -main + ([] + (comment run all)) + ([day] + (let [day-str (str "day" (format "%02d" (Integer/parseInt day)))] + (eval (read-string (format "(adventofcode2019.%s/%s)" day-str day-str)))))) diff --git a/day01/fuel.clj b/src/adventofcode2019/day01.clj similarity index 60% rename from day01/fuel.clj rename to src/adventofcode2019/day01.clj index 916f45b..5192833 100644 --- a/day01/fuel.clj +++ b/src/adventofcode2019/day01.clj @@ -1,11 +1,6 @@ -(ns aoc.core - [:require [clojure.string :as str]]) - -(def get-list-from-file - #(str/split-lines (slurp %))) - -(def parse-int - #(Integer/parseInt %)) +(ns adventofcode2019.day01 + [:require [adventofcode2019.lib :refer :all] + [clojure.string :as str]]) (defn get-fuel-req [mass] (reduce + (take-while #(> % 0) (drop 1 (iterate #(- (quot % 3) 2) mass))))) @@ -16,9 +11,7 @@ (defn total-fuel-requirement [input] (reduce + (map get-fuel-req input))) -(defn -main [] - (let [input (map parse-int (get-list-from-file "input"))] +(defn day01 [] + (let [input (map parse-int (get-list-from-file (input-file)))] (println (total-fuel-requirement-naive input)) (println (total-fuel-requirement input)))) - -(-main) diff --git a/src/adventofcode2019/day02.clj b/src/adventofcode2019/day02.clj new file mode 100644 index 0000000..574d799 --- /dev/null +++ b/src/adventofcode2019/day02.clj @@ -0,0 +1,32 @@ +(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 diff --git a/src/adventofcode2019/lib.clj b/src/adventofcode2019/lib.clj new file mode 100644 index 0000000..e846216 --- /dev/null +++ b/src/adventofcode2019/lib.clj @@ -0,0 +1,16 @@ +(ns adventofcode2019.lib + [:require [clojure.string :as str] + [clojure.java.io :as io]]) + +(defn get-list-from-file + ([file-name] + (str/split-lines (str/trim (slurp file-name)))) + ([file-name split-regex] + (str/split (str/trim (slurp file-name)) split-regex))) + +(defmacro input-file [] + (let [bottom-ns (last (str/split (str *ns*) #"\."))] + (str "resources/" bottom-ns))) + +(def parse-int + #(Integer/parseInt %)) diff --git a/src/adventofcode2019/template.clj b/src/adventofcode2019/template.clj new file mode 100644 index 0000000..2e58ab0 --- /dev/null +++ b/src/adventofcode2019/template.clj @@ -0,0 +1,8 @@ +(ns adventofcode2019.day00 + [:require [adventofcode2019.lib :refer :all] + [clojure.string :as str] + [clojure.math.combinatorics :as combo]]) + +(defn day00 [] + (let [input (map parse-int (get-list-from-file (input-file)))] + (comment Your code here!))) diff --git a/test/adventofcode2019/core_test.clj b/test/adventofcode2019/core_test.clj new file mode 100644 index 0000000..d571560 --- /dev/null +++ b/test/adventofcode2019/core_test.clj @@ -0,0 +1,7 @@ +(ns adventofcode2019.core-test + (:require [clojure.test :refer :all] + [adventofcode2019.core :refer :all])) + +(deftest a-test + (testing "FIXME, I fail." + (is (= 0 1)))) -- 2.26.2