src/2019/days/day-08.lisp @ 5f6c2d777533

2019/08 and fix some test failures
author Steve Losh <steve@stevelosh.com>
date Sun, 08 Dec 2019 13:47:53 -0500
parents (none)
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 --------------------------------------------------------------------