diff --git a/day8/part2.clj b/day8/part2.clj index 7fbaf92..1ee3c08 100644 --- a/day8/part2.clj +++ b/day8/part2.clj @@ -8,17 +8,17 @@ digits in `cnts` with `grp-to-cmp` segments. " [segs-left dig-to-cmp cnts grp-to-cmp] - (second - (first - (filter - #(-> (second %) - (str/replace (re-pattern (str/join ["[" dig-to-cmp "]"])) "") - (count) - (= segs-left) - ) - (filterv #(= (first %) grp-to-cmp) cnts) - ) + (as-> cnts $ + (filterv #(= (first %) grp-to-cmp) $) + (filterv + #(-> (second %) + (str/replace (re-pattern (str/join ["[" dig-to-cmp "]"])) "") + (count) + (= segs-left) + ) + $ ) + (get-in $ [0 1]) ) ) @@ -31,35 +31,36 @@ 7 (mcounts 3) 8 (mcounts 7) ) - (assoc $ 3 (find-match 2 ($ 7) counts 5)) - (assoc $ 2 (find-match 3 ($ 4) counts 5)) + (assoc $ 3 (find-match 2 ($ 7) counts 5) + 6 (find-match 4 ($ 7) counts 6) + 2 (find-match 3 ($ 4) counts 5) + 9 (find-match 2 ($ 4) counts 6) + ) (assoc $ 5 (find-match 2 ($ 2) counts 5)) (assoc $ 0 (find-match 2 ($ 5) counts 6)) - (assoc $ 9 (find-match 2 ($ 4) counts 6)) - (assoc $ 6 (find-match 4 ($ 7) counts 6)) ) ) ) -(loop [sum 0] - (let [rl (read-line)] - (if (empty? rl) - (println sum) - (let [line (as-> rl $ +(println + (reduce + (fn [sum input] + (let [line (as-> input $ (str/split $ #" ") (mapv (comp str/join sort) $) ) number (subvec line 11 15) decoder (set/map-invert (determine-digits line))] - (recur (->> number - (map (comp str decoder)) - (str/join) - (#(Integer/parseInt %)) - (+ sum) - ) - ) + (->> number + (map (comp str decoder)) + (str/join) + (#(Integer/parseInt %)) + (+ sum) + ) ) ) + 0 + (str/split-lines (slurp "./in")) ) )