day8: improve part2

master
Clyne 3 years ago
parent cdde63a797
commit 85d2aae745

@ -8,17 +8,17 @@
digits in `cnts` with `grp-to-cmp` segments.
"
[segs-left dig-to-cmp cnts grp-to-cmp]
(second
(first
(filter
(as-> cnts $
(filterv #(= (first %) grp-to-cmp) $)
(filterv
#(-> (second %)
(str/replace (re-pattern (str/join ["[" dig-to-cmp "]"])) "")
(count)
(= segs-left)
)
(filterv #(= (first %) grp-to-cmp) cnts)
)
$
)
(get-in $ [0 1])
)
)
@ -31,27 +31,27 @@
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
(->> number
(map (comp str decoder))
(str/join)
(#(Integer/parseInt %))
@ -59,7 +59,8 @@
)
)
)
)
0
(str/split-lines (slurp "./in"))
)
)

Loading…
Cancel
Save