Finish day2, add project structure
authorJack Kinsey <j.jameskinsey@gmail.com>
Tue, 3 Dec 2019 01:43:00 +0000 (20:43 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Tue, 3 Dec 2019 01:43:00 +0000 (20:43 -0500)
.gitignore [new file with mode: 0644]
day02/intcode.clj [deleted file]
project.clj [new file with mode: 0644]
resources/day01 [moved from day01/input with 100% similarity]
resources/day02 [new file with mode: 0644]
src/adventofcode2019/core.clj [new file with mode: 0644]
src/adventofcode2019/day01.clj [moved from day01/fuel.clj with 60% similarity]
src/adventofcode2019/day02.clj [new file with mode: 0644]
src/adventofcode2019/lib.clj [new file with mode: 0644]
src/adventofcode2019/template.clj [new file with mode: 0644]
test/adventofcode2019/core_test.clj [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..d18f225
--- /dev/null
@@ -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 (file)
index b21ffd2..0000000
+++ /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 (file)
index 0000000..fe88a58
--- /dev/null
@@ -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}})
similarity index 100%
rename from day01/input
rename to resources/day01
diff --git a/resources/day02 b/resources/day02
new file mode 100644 (file)
index 0000000..99d3840
--- /dev/null
@@ -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 (file)
index 0000000..0e47a72
--- /dev/null
@@ -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))))))
similarity index 60%
rename from day01/fuel.clj
rename to src/adventofcode2019/day01.clj
index 916f45b73361f06e5d2a89f352aec8f9fea4fd64..5192833885082bea1fea29fd7764998899a6064e 100644 (file)
@@ -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 (file)
index 0000000..574d799
--- /dev/null
@@ -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 (file)
index 0000000..e846216
--- /dev/null
@@ -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 (file)
index 0000000..2e58ab0
--- /dev/null
@@ -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 (file)
index 0000000..d571560
--- /dev/null
@@ -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))))