]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day3: little more touch-up
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 5 Dec 2021 00:54:16 +0000 (19:54 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 5 Dec 2021 00:54:16 +0000 (19:54 -0500)
day3/part1.clj
day3/part2.clj

index 915ee47e50fdb8bf6195ab6b47d1ab74e8126188..3fe888ef371f1ae7f80f7c40cac35b0db6ebfec5 100644 (file)
@@ -1,17 +1,16 @@
 (require '[clojure.string :as str])
 
-(println
-  (->> "./in"
-       (slurp)
-       (str/split-lines)
-       (map (fn [l] (map #(if (= % \1) 1 0) l)))
-       (apply (partial map +))
-       (map #(if (< % 500) \1 \0))
-       (str/join)
-       (#(Integer/parseInt % 2))
-       (#(* % (bit-xor % (dec (int (Math/pow 2 12))))))
-       )
-  )
+(->> "./in"
+     (slurp)
+     (str/split-lines)
+     (map (fn [l] (map #(if (= % \1) 1 0) l)))
+     (apply (partial map +))
+     (map #(if (< % 500) \1 \0))
+     (str/join)
+     (#(Integer/parseInt % 2))
+     (#(* % (bit-xor % (dec (int (Math/pow 2 12))))))
+     (println)
+     )
 
 ; (->> input data file name
 ;      read in entire contents
@@ -22,4 +21,5 @@
 ;      join characters into single string
 ;      convert binary string to a number (gamma)
 ;      multiply gamma by its bit-inverse (bit length hard-coded)
+;      print results
 
index cbbe35f5822829a4d074f8b1f570f6618a5488c3..00e656faee7caeb430f9c7e2c5dbebd8bda60805 100644 (file)
@@ -1,13 +1,12 @@
+(require '[clojure.string :as str])
+
 (def bitcount 12)
 (def input
-  (loop [tot []]
-    (let [line (read-line)]
-      (if (empty? line)
-        tot
-        (recur (conj tot (Integer/parseInt line 2)))
-        )
+  (->> "./in"
+      (slurp)
+      (str/split-lines)
+      (map #(Integer/parseInt % 2))
       )
-    )
   )
 
 (defn countbit [lst bit]
        (apply +)
        )
   )
+
 (defn filterbit [lst bit v]
   (if (= 1 (count lst))
     lst
-    (loop [l lst r []]
-      (if (empty? l)
-        r
-        (recur
-          (rest l)
-          (if (= (bit-test (first l) bit) v)
-            (conj r (first l))
-            r
-            )
-          )
-        )
-      )
+    (filter #(= (bit-test % bit) v) lst)
     )
   )
 
 (loop [bit (dec bitcount) lst0 input lst1 input]
   (if (and (= 1 (count lst0)) (= 1 (count lst1)))
-    (println (* (first lst0) (first lst1)))
+    (println (map * lst0 lst1))
     (recur
       (dec bit)
       (filterbit lst0 bit (>= (countbit lst0 bit) (/ (count lst0) 2)))