day3: little more touch-up

master
Clyne 3 years ago
parent f413e05528
commit d9394b84b1

@ -1,7 +1,6 @@
(require '[clojure.string :as str])
(println
(->> "./in"
(->> "./in"
(slurp)
(str/split-lines)
(map (fn [l] (map #(if (= % \1) 1 0) l)))
@ -10,7 +9,7 @@
(str/join)
(#(Integer/parseInt % 2))
(#(* % (bit-xor % (dec (int (Math/pow 2 12))))))
)
(println)
)
; (->> input data file name
@ -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

@ -1,12 +1,11 @@
(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))
)
)
@ -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)))

Loading…
Cancel
Save