(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)))))