day3: part 1 one-liner
parent
49b77e8fd5
commit
ebd7f129af
@ -1,31 +1,25 @@
|
|||||||
(def counts
|
(require '[clojure.string :as str])
|
||||||
(loop [line (read-line)
|
|
||||||
tot (repeat (count line) 0)
|
|
||||||
]
|
|
||||||
(if (empty? line)
|
|
||||||
tot
|
|
||||||
(recur
|
|
||||||
(read-line)
|
|
||||||
(map + tot (map #(if (= % \1) 1 0) line))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
(loop [cnts counts gamma 0 epsilon 0]
|
(println
|
||||||
(if (empty? cnts)
|
(->> "./in"
|
||||||
(println (* gamma epsilon))
|
(slurp)
|
||||||
(recur
|
(str/split-lines)
|
||||||
(rest cnts)
|
(map (fn [l] (map #(if (= % \1) 1 0) l)))
|
||||||
(if (> (first cnts) 500)
|
(apply (partial map +))
|
||||||
(inc (* 2 gamma))
|
(map #(if (< % 500) \1 \0))
|
||||||
(* 2 gamma)
|
(str/join)
|
||||||
)
|
(#(Integer/parseInt % 2))
|
||||||
(if (< (first cnts) 500)
|
(#(* % (bit-xor % (dec (int (Math/pow 2 12))))))
|
||||||
(inc (* 2 epsilon))
|
)
|
||||||
(* 2 epsilon)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
; (->> input data file name
|
||||||
|
; read in entire contents
|
||||||
|
; split contents into array of lines
|
||||||
|
; for each line, transform characters '1'/'0' to numbers
|
||||||
|
; build sum array using the lines
|
||||||
|
; convert back to array of characters
|
||||||
|
; join characters into single string
|
||||||
|
; convert binary string to a number (gamma)
|
||||||
|
; multiply gamma by its bit-inverse (bit length hard-coded)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue