aboutsummaryrefslogtreecommitdiffstats
path: root/src/aoc_2022
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-12-04 15:14:47 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-04 17:02:06 +0200
commit1adba42708436a86db1f369028fb6fae4bc4eda8 (patch)
tree7ecf237e098b6886ae451c04d58e5a79718b3e19 /src/aoc_2022
parent576d3eae2d75a05b8191d7168e19ff71a8c3b0e0 (diff)
Add util fns
Diffstat (limited to 'src/aoc_2022')
-rw-r--r--src/aoc_2022/day04.clj19
-rw-r--r--src/aoc_2022/day05.clj10
-rw-r--r--src/aoc_2022/utils.clj54
3 files changed, 74 insertions, 9 deletions
diff --git a/src/aoc_2022/day04.clj b/src/aoc_2022/day04.clj
index a6ca91b..bd4d562 100644
--- a/src/aoc_2022/day04.clj
+++ b/src/aoc_2022/day04.clj
@@ -1,14 +1,14 @@
(ns aoc-2022.day04)
+(require '[aoc-2022.utils :refer :all])
(require '[clojure.java.io :as io])
-(require '[aoc-2022.utils :as utils])
(require '[clojure.string :as str])
(defn parse-rows [rows]
(->> rows
(map #(str/split % #","))
(map (fn [[a, b]]
- [(map #(Long/valueOf %) (str/split a #"-")),
- (map #(Long/valueOf %) (str/split b #"-"))]))
+ [(map to-long (str/split a #"-")),
+ (map to-long (str/split b #"-"))]))
(map flatten)))
(defn assignment-contains [[a1 a2 b1 b2]]
@@ -25,14 +25,15 @@
(defn assignment-overlaps [[a1 a2 b1 b2]]
(not (or (> a1 b2) (> b1 a2))))
-(defn task2 [rows]
- (->> rows
- parse-rows
- (filter assignment-overlaps)
- count))
+(def task2
+ (chain
+ parse-rows
+ (partial filter assignment-overlaps)
+ count))
(with-open [rdr (io/reader "resources/day04_actual.txt")]
(let [rows (line-seq rdr)]
(do
(println "task1:" (task1 rows))
- (println "task2:" (task2 rows)))))
+ (println "task2:" (task2 rows))
+ :done)))
diff --git a/src/aoc_2022/day05.clj b/src/aoc_2022/day05.clj
new file mode 100644
index 0000000..fcfda0b
--- /dev/null
+++ b/src/aoc_2022/day05.clj
@@ -0,0 +1,10 @@
+(ns aoc-2022.day05)
+(require '[aoc-2022.utils :refer :all])
+
+(def task1
+ (chain))
+
+(def task2
+ (chain))
+
+(run-aoc "day05" task1 task2)
diff --git a/src/aoc_2022/utils.clj b/src/aoc_2022/utils.clj
index 57bfa88..4ade901 100644
--- a/src/aoc_2022/utils.clj
+++ b/src/aoc_2022/utils.clj
@@ -1,3 +1,57 @@
(ns aoc-2022.utils)
+(require '[clojure.string :as str])
+
+; Debugging
(def spy #(do (println "DEBUG:" %) %))
+
+; Number parsing
+
+(def to-long #(Long/valueOf %))
+(def to-float #(Float/valueOf %))
+
+; Collections
+
+(defn- split-by' [delim acc [x & xs]]
+ (condp = x
+ nil (list (reverse acc))
+ delim (cons (reverse acc) (split-by' delim '() xs))
+ ; otherwise
+ (split-by' delim (cons x acc) xs)))
+
+(def split-by #(split-by' %1 '() %2))
+
+; Advent of Code specific
+
+(defn read-aoc [day]
+ (let [slurp-f #(str/split-lines (slurp (format % day)))]
+ {:sample (slurp-f "resources/%s_sample.txt")
+ :actual (slurp-f "resources/%s_actual.txt")}))
+
+(defn run-aoc
+ "Reads input files for `day` and invokes given `task1` and `task2` functions with the contents.
+ `task1` and `task2` should take a list of strings as their first argument."
+ [day task1 task2]
+ (let [{:keys [sample actual]} (read-aoc day)]
+ (do
+ (println "task1 @ sample:" (task1 sample))
+ (println "task1 @ actual:" (task1 actual))
+ (println "task2 @ sample:" (task2 sample))
+ (println "task2 @ actual:" (task2 actual)))))
+
+; Functions
+
+(defn chain "forward compose: comp fns in reverse order" [& fns]
+ (apply comp (reverse fns)))
+
+; Logic
+
+(defmacro nand
+ "Logical NAND. `(nand a b c)` is equivalent to `(not (and a b c))`."
+ [& args]
+ (list 'not (cons 'and args)))
+
+(defmacro nor
+ "Logical NOR. `(nor a b c)` is equivalent to `(not (or a b c))`."
+ [& args]
+ (list 'not (cons 'or args)))