aboutsummaryrefslogtreecommitdiffstats
path: root/day3/part2.clj
blob: 00e656faee7caeb430f9c7e2c5dbebd8bda60805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)))
      )
    )
  )