aboutsummaryrefslogtreecommitdiffstats
path: root/day3/part2.clj
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-04 19:54:16 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-04 19:54:16 -0500
commitd9394b84b11b84ed5a5439a617e9aa4e4126637f (patch)
tree8ac928b5e57e5d588529bb5a1c5e6898a7fee916 /day3/part2.clj
parentf413e055286d1b7877b3512cf3170d928d8a1ee7 (diff)
day3: little more touch-up
Diffstat (limited to 'day3/part2.clj')
-rw-r--r--day3/part2.clj29
1 files changed, 9 insertions, 20 deletions
diff --git a/day3/part2.clj b/day3/part2.clj
index cbbe35f..00e656f 100644
--- a/day3/part2.clj
+++ b/day3/part2.clj
@@ -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]
@@ -16,27 +15,17 @@
(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)))