


#lang scribble/lp2




@(require scribble/manual aocracket/helper)








@aoctitle[2]








@defmodule[aocracket/day02]








@link["http://adventofcode.com/day/2"]{The puzzle}. Our @linkrp["day02input.txt"]{input} is a list of strings that represent dimensions of rectangular boxes.








@chunk[<day02>




<day02setup>




<day02q1>




<day02test>]












@isection{How much paper is needed to wrap the boxes?}








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 onetwo punch of @iracket[map] and @iracket[apply]. We'll write a function to compute surface area from box dimensions. Then we'll @racket[map] that function across the list of boxes, and finally @racket[apply] the @racket[+] operator to our list of results to get the answer.












@chunk[<day02setup>




(require racket rackunit)




(provide (alldefinedout))




(define (string>boxes str)




(for/list ([ln (inlist (stringsplit str "\n"))])




(map string>number (stringsplit ln "x"))))]








@chunk[<day02q1>




(define (box>paper box)




(matchdefine (list x y z) box)




(define sides (list (* x y) (* y z) (* x z)))




(+ (* 2 (apply + sides)) (apply min sides)))








(define (q1 str)




(define boxes (string>boxes str))




(apply + (map box>paper boxes)))]








@section{How much ribbon is needed to wrap the boxes?}








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 @racket[box>ribbon] function.








@chunk[<day02q1>




(define (box>ribbon box)




(matchdefine (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)))]












@section{Testing Day 2}












@chunk[<day02test>




(module+ test




(define inputstr (file>string "day02input.txt"))




(checkequal? (q1 inputstr) 1586300)




(checkequal? (q2 inputstr) 3737498))]
