aboutsummaryrefslogtreecommitdiffstats
path: root/day3/part2.clj
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-03 09:20:32 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-03 09:20:32 -0500
commit897410ff8148cda1d9742213b7f775dbb6f55238 (patch)
tree049d0aeacfbdb33e6fd94e75b5c704abd011969f /day3/part2.clj
parent09e1c2712b76bf35aa6beeb8709f22911dc494d3 (diff)
add day3
Diffstat (limited to 'day3/part2.clj')
-rw-r--r--day3/part2.clj47
1 files changed, 47 insertions, 0 deletions
diff --git a/day3/part2.clj b/day3/part2.clj
new file mode 100644
index 0000000..cbbe35f
--- /dev/null
+++ b/day3/part2.clj
@@ -0,0 +1,47 @@
+(def bitcount 12)
+(def input
+ (loop [tot []]
+ (let [line (read-line)]
+ (if (empty? line)
+ tot
+ (recur (conj tot (Integer/parseInt line 2)))
+ )
+ )
+ )
+ )
+
+(defn countbit [lst bit]
+ (->> lst
+ (map #(if (bit-test % bit) 1 0))
+ (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
+ )
+ )
+ )
+ )
+ )
+ )
+
+(loop [bit (dec bitcount) lst0 input lst1 input]
+ (if (and (= 1 (count lst0)) (= 1 (count lst1)))
+ (println (* (first lst0) (first lst1)))
+ (recur
+ (dec bit)
+ (filterbit lst0 bit (>= (countbit lst0 bit) (/ (count lst0) 2)))
+ (filterbit lst1 bit (< (countbit lst1 bit) (/ (count lst1) 2)))
+ )
+ )
+ )
+