]> localhost Git - adventofcode2019.git/commitdiff
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]
day01/input [deleted file]
day02/intcode.clj [deleted file]
project.clj [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/day01/input b/day01/input
deleted file mode 100644 (file)
index eea337c..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-95065
-129298
-145573
-95743
-59139
-78323
-124445
-69015
-81990
-83254
-139274
-92101
-74245
-104038
-61955
-80642
-110376
-89992
-84392
-117830
-140144
-80076
-111285
-107135
-98741
-103753
-141922
-130503
-60409
-73891
-84781
-118319
-93610
-143228
-99616
-65353
-102388
-123813
-88335
-95459
-133635
-108771
-101999
-73850
-106490
-53396
-110330
-140258
-73958
-60273
-101401
-128995
-61495
-114674
-71955
-107049
-79374
-52359
-107925
-91789
-69174
-133966
-85063
-62856
-96965
-97100
-81638
-104488
-131368
-59015
-149357
-65193
-61489
-126089
-141224
-100596
-93144
-109421
-121988
-135890
-70141
-53531
-59900
-98981
-66796
-113700
-109535
-100721
-87240
-99883
-81637
-80064
-143154
-75778
-64835
-59235
-103907
-121637
-118525
-125730
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}})
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))))