Racket solutions & explanations for the Advent of Code puzzles
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pullrequests.


#lang scribble/lp2



@(require scribble/manual aocracket/helper)






@aoctitle[17]






@defmodule[aocracket/day17]






@link["http://adventofcode.com/day/17"]{The puzzle}. Our @linkrp["day17input.txt"]{input} is a list of containers that hold the designated number of liters.









@chunk[<day17>



<day17setup>



<day17q1>



<day17q2>



<day17test>]






@section{How many combinations of containers fit exactly 150 liters?}






This is a lot like the second part of @secref{Day_15}, where we had to find cookie recipes that totaled 500 calories. This time, rather than recipes, we need to generate combinations of the containers that add up to exactly 150 liters (though we don't have to use all the containers, and multiple containers of the same size are deemed to create unique arrangements).






We do this by creating the @italic{power set} of the containers — that is, a list of all possible subsets — and counting how many meet our criterion. As with the recipe problem, our @racket[powerset] function is a simple recursive operation.






@chunk[<day17setup>



(require racket rackunit)



(provide (alldefinedout))






(define (powerset xs)



(if (empty? xs)



(list empty)



(appendmap



(λ (s) (list (cons (car xs) s) s))



(powerset (cdr xs)))))



]






@chunk[<day17q1>



(define (q1 inputstr)



(define containers



(map string>number (stringsplit inputstr)))



(length (filter (λ (s) (= 150 (apply + s)))



(powerset containers))))]












@section{How many combinations have the minimum number of containers?}






Same as above, except we find the minimum length among the winners, and then count how many other winners have that length.






@chunk[<day17q2>






(define (q2 inputstr)



(define containers



(map string>number (stringsplit inputstr)))



(let* ([winners (filter (λ (s) (= 150 (apply + s)))



(powerset containers))]



[shortest (apply min (map length winners))])



(length (filter (λ (w) (= shortest (length w))) winners))))






]












@section{Testing Day 17}






@chunk[<day17test>



(module+ test



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



(checkequal? (q1 inputstr) 1638)



(checkequal? (q2 inputstr) 17))]









