Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created December 3, 2021 14:12
Show Gist options
  • Save roman01la/6368314f12e60474590808c1993e8b9d to your computer and use it in GitHub Desktop.
Save roman01la/6368314f12e60474590808c1993e8b9d to your computer and use it in GitHub Desktop.
(defn part-1 [input]
(->> input
str/split-lines
(map seq)
(apply map list)
(map #(vals (into (sorted-map) (set/map-invert (frequencies %)))))
(apply map list)
(map (comp #(Integer/parseInt % 2) str/join))
(apply *)))
(defn get-rating [input idx n]
(let [fbits (nth (apply mapv list input) n)
m (frequencies fbits)
m (cond
(not (get m \0)) (assoc m \0 0)
(not (get m \1)) (assoc m \1 1)
:else m)
v (vals (into (sorted-map) (set/map-invert m)))
x (cond
(and (= 1 (count v)) (= 0 idx)) \0
(and (= 1 (count v)) (= 1 idx)) \1
:else (nth v idx))
input-next (filter #(= x (nth % n)) input)]
(if (= 1 (count input-next))
(-> (first input-next) str/join (Integer/parseInt 2))
(recur input-next idx (inc n)))))
(defn part-2 [input]
(let [parsed-input (->> input str/split-lines (map (comp vec seq)))]
(* (get-rating parsed-input 0 0)
(get-rating parsed-input 1 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment