From: Jack Kinsey Date: Sun, 8 Dec 2019 19:08:15 +0000 (-0500) Subject: Tidy up day8 a little bit X-Git-Url: http://git.jkinsey.net/?p=adventofcode2019.git;a=commitdiff_plain;h=103ead17b1fbf36b0c1d464d0372b9831de6a649 Tidy up day8 a little bit --- diff --git a/src/adventofcode2019/day08.clj b/src/adventofcode2019/day08.clj index a3aa45e..d27d718 100644 --- a/src/adventofcode2019/day08.clj +++ b/src/adventofcode2019/day08.clj @@ -1,25 +1,27 @@ (ns adventofcode2019.day08 [:require [adventofcode2019.lib :refer :all] + [clojure.string :as str] [clojure.core.match :refer [match]]]) -(def combine-layers - "Binary operation on the pixels of two layers, the first above the second." - #(match [%1 %2] - [0 _] 0 - [1 _] 1 - [2 p] p)) - (defn day08 [] - (let [input (mapv parse-int (get-list-from-file (input-file) #"")) - layer-size (* 25 6) + (let [input (map parse-int (get-list-from-file (input-file) #"")) + [image-x image-y] [25 6] + layer-size (* image-x image-y) + count-num #(count (filter (hash-set %1) %2)) + combine-layers #(match [%1 %2] [0 _] 0 + [1 _] 1 + [2 p] p) + to-text #(match % 0 \u0020 ; 0s are spaces, 1s are full-block + 1 \u2588) layers (partition layer-size input) - fewest-zeroes (apply (partial min-key (partial count-num 0)) - layers) - layered-image (reduce #(mapv combine-layers %1 %2) layers) - to-ascii #(match % 0 \u0020 ; 0s are spaces, 1s are full-block - 1 \u2588)] + + fewest-zeroes (apply (partial min-key (partial count-num 0)) layers) + layered-image (reduce #(map combine-layers %1 %2) layers)] (part1 (* (count-num 1 fewest-zeroes) (count-num 2 fewest-zeroes))) (part2 "see below") - (run! println (partition 25 (map to-ascii layered-image))))) + (run! println (->> layered-image + (map to-text) + (partition image-x) + (map str/join)))))