Challenge with images: Racket: `it's showtime`

This commit is contained in:
xenia 2019-12-08 00:33:00 -05:00
parent d92abf86ec
commit 1755df6a1f
1 changed files with 50 additions and 0 deletions

50
8.rkt Normal file
View File

@ -0,0 +1,50 @@
#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)