src/2019/days/day-08.lisp @ d692b61fbee1
More test fixes
author |
Steve Losh <steve@stevelosh.com> |
date |
Sun, 08 Dec 2019 14:50:54 -0500 |
parents |
5f6c2d777533 |
children |
ebd2a1bb4889 |
(defpackage :advent/2019/08 #.cl-user::*advent-use*)
(in-package :advent/2019/08)
(defun read-layer (stream width height)
(let-result (layer (make-array (list height width)))
(do-range ((y 0 height)
(x 0 width))
(setf (aref layer y x) (read-char stream)))))
(defun read-image (stream width height)
(iterate (until (char= #\newline (peek-char nil stream nil #\newline)))
(collect (read-layer stream width height))))
(defun count-digit (layer digit)
(iterate (for d :across-flat-array layer)
(counting (char= digit d))))
(defun pixel-color (layers y x)
(iterate (for layer :in layers)
(thereis (ecase (aref layer y x)
(#\2) ; transparent
(#\1 1)
(#\0 0)))))
(defun decode-image (layers)
(destructuring-bind (height width) (array-dimensions (first layers))
(let-result (image (make-array (list width height)))
(do-range ((y 0 height)
(x 0 width))
(setf (aref image x y)
(pixel-color layers y x))))))
(define-problem (2019 8) (stream) (1806)
(let ((image (read-image stream 25 6)))
(netpbm:write-to-file "out/2019-08.pbm" (decode-image image)
:if-exists :supersede
:format :pbm)
(iterate
(for layer :in image)
(finding (* (count-digit layer #\1)
(count-digit layer #\2))
:minimizing (count-digit layer #\0)))))
#; Scratch --------------------------------------------------------------------