]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day4: try cleaning up predicates
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 4 Dec 2022 13:22:52 +0000 (08:22 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 4 Dec 2022 13:22:52 +0000 (08:22 -0500)
day4/both.clj

index a59098ad4b3a58a05d93ec8dedaec5256d8bcbcf..441ec16e4c71a097841b53e844044e3769a17612 100644 (file)
@@ -1,14 +1,14 @@
+(defn within [n b e] (and (>= n b) (<= n e)))
+
 (defn part1pred [lst]
-  (or (and (>= (nth lst 0) (nth lst 2))
-           (<= (nth lst 1) (nth lst 3)))
-      (and (>= (nth lst 2) (nth lst 0))
-           (<= (nth lst 3) (nth lst 1)))))
+  (or (and (>= (nth lst 0) (nth lst 2)) (<= (nth lst 1) (nth lst 3)))
+      (and (>= (nth lst 2) (nth lst 0)) (<= (nth lst 3) (nth lst 1)))))
 
 (defn part2pred [lst]
-  (or (and (>= (nth lst 0) (nth lst 2)) (<= (nth lst 0) (nth lst 3)))
-      (and (>= (nth lst 1) (nth lst 2)) (<= (nth lst 1) (nth lst 3)))
-      (and (>= (nth lst 2) (nth lst 0)) (<= (nth lst 2) (nth lst 1)))
-      (and (>= (nth lst 3) (nth lst 0)) (<= (nth lst 3) (nth lst 1)))))
+  (or (within (nth lst 0) (nth lst 2) (nth lst 3))
+      (within (nth lst 1) (nth lst 2) (nth lst 3))
+      (within (nth lst 2) (nth lst 0) (nth lst 1))
+      (within (nth lst 3) (nth lst 0) (nth lst 1))))
 
 (def count-filtered (comp println count filter))