|(require aoc-racket/day02)||package: aoc-racket|
According to the problem, the paper needed to wrap a present is the surface area of the box (= the sum of the areas of the sides) plus the area of the smallest side.
First we need to parse our input file into a list of box dimensions. We’ll model each box as a list of three dimensions. (The question doesn’t need us to keep height / width / depth straight, so we won’t worry about it.)
Then we have a traditional setup for the devastating one-two punch of map and apply. We’ll write a function to compute surface area from box dimensions. Then we’ll map that function across the list of boxes, and finally apply the + operator to our list of results to get the answer.
(require racket rackunit) (provide (all-defined-out)) (define (string->boxes str) (for/list ([ln (in-list (string-split str "\n"))]) (map string->number (string-split ln "x"))))
According to the problem, the ribbon needed is the perimeter of the smallest side plus the volume of the box.
We take the same approach, with a new box->ribbon function.
(define (box->ribbon box) (match-define (list x y z) box) (define (perimeter dim1 dim2) (* 2 (+ dim1 dim2))) (define perimeters (list (perimeter x y) (perimeter y z) (perimeter x z))) (+ (apply min perimeters) (* x y z))) (define (q2 str) (define boxes (string->boxes str)) (apply + (map box->ribbon boxes)))