aoc2019/8.rkt

50 lines
1.4 KiB
Racket
Raw Normal View History

#lang racket
(define input (for/list ([str (string-trim (file->string "input.8.txt"))])
(string->number (string str))))
(define width 25)
(define height 6)
(define pixels-per-layer (* width height))
(define (make-chunks lst n)
(cond [(empty? lst) '()]
[else (cons (take lst n) (make-chunks (drop lst n) n))]))
(define layers (make-chunks input pixels-per-layer))
(define (count-occur lst what)
(length (filter ((curry =) what) lst)))
(define (lowest-zeroes chunks)
(cond [(empty? (rest chunks)) (first chunks)]
[else
(let ([this (first chunks)]
[next (lowest-zeroes (rest chunks))])
(if (< (count-occur this 0) (count-occur next 0))
this
next))]))
(define lowest-zero-layer (lowest-zeroes layers))
;; Part 1
(* (count-occur lowest-zero-layer 1) (count-occur lowest-zero-layer 2))
(require 2htdp/image)
(define pixel-size 16)
(define colors '(black white transparent))
(define (render-pixel code)
(rectangle pixel-size pixel-size 'solid (list-ref colors code)))
(define (render-layer layer)
(define (render-row row)
(apply beside (map render-pixel row)))
(apply above (map render-row (make-chunks layer width))))
(define base (empty-scene (* pixel-size width) (* pixel-size height)))
(define decoded-image
(foldr overlay base (map render-layer layers)))
;; Part 2
(frame decoded-image)