From 8d43e37df99f280377bed90284d6ac2428334804 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 30 Nov 2022 19:55:31 -0500 Subject: move 2021 days to folder; update README --- year2021/day3/part2.clj | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 year2021/day3/part2.clj (limited to 'year2021/day3/part2.clj') diff --git a/year2021/day3/part2.clj b/year2021/day3/part2.clj new file mode 100644 index 0000000..00e656f --- /dev/null +++ b/year2021/day3/part2.clj @@ -0,0 +1,36 @@ +(require '[clojure.string :as str]) + +(def bitcount 12) +(def input + (->> "./in" + (slurp) + (str/split-lines) + (map #(Integer/parseInt % 2)) + ) + ) + +(defn countbit [lst bit] + (->> lst + (map #(if (bit-test % bit) 1 0)) + (apply +) + ) + ) + +(defn filterbit [lst bit v] + (if (= 1 (count lst)) + lst + (filter #(= (bit-test % bit) v) lst) + ) + ) + +(loop [bit (dec bitcount) lst0 input lst1 input] + (if (and (= 1 (count lst0)) (= 1 (count lst1))) + (println (map * lst0 lst1)) + (recur + (dec bit) + (filterbit lst0 bit (>= (countbit lst0 bit) (/ (count lst0) 2))) + (filterbit lst1 bit (< (countbit lst1 bit) (/ (count lst1) 2))) + ) + ) + ) + -- cgit v1.2.3