[download] []
#!/usr/bin/env -S clojure -M
(load-file "/home/me/aoc/aoc.clj")
(defs year 2021 day 8 part 2 test_ 0)
(def lines (rex #"(?m)^(.+) \| (.+)$" [words words] (aoci)))
(def freq
(->> "cagedb ab gcdfa fbcad eafb cdfbe cdfgeb dab acedgfb cefabd"
frequencies
(#(dissoc % \space))))
(def fmap
(->> "cagedb ab gcdfa fbcad eafb cdfbe cdfgeb dab acedgfb cefabd"
words
(map (c map (fn [char] (get freq char))))
(map sort)))
(defn list-index-of [x coll]
(let [idx? (fn [i a] (when (= x a) i))]
(first (keep-indexed idx? coll))))
(def out
(->> lines
(map
(fn [[nums final]]
(let [freq
(->> nums
(apply str)
frequencies)
fmap
(->> nums
(map (fn [word] [(join (sort word)) (list-index-of (sort (map (fn [char] (get freq char)) word)) fmap)]))
flatten
(apply hash-map))]
(->> final
(map sort)
(map join)
(map (c get fmap))
(apply str)
atoi))))
sum))
(print out "\n")
(aoco out)