diff --git a/day16.rkt b/day16.rkt index b53dad5..60a750c 100644 --- a/day16.rkt +++ b/day16.rkt @@ -61,7 +61,7 @@ We might be tempted to break down the attribute pairs into hash tables. But we d -@section{Which Sue matches the attribute input, with the retroencabulator rules?} +@section{Which Sue matches the attribute input, with the ``retroencabulator'' rules?} Same question as before, with new rules for matching the master attributes: diff --git a/day17-input.txt b/day17-input.txt new file mode 100644 index 0000000..02f67bd --- /dev/null +++ b/day17-input.txt @@ -0,0 +1,20 @@ +43 +3 +4 +10 +21 +44 +4 +6 +47 +41 +34 +17 +17 +44 +36 +31 +46 +9 +27 +38 \ No newline at end of file diff --git a/day17.rkt b/day17.rkt new file mode 100644 index 0000000..dda4bc8 --- /dev/null +++ b/day17.rkt @@ -0,0 +1,70 @@ +#lang scribble/lp2 +@(require scribble/manual aoc-racket/helper) + +@aoc-title[17] + +@defmodule[aoc-racket/day17] + +@link["http://adventofcode.com/day/17"]{The puzzle}. Our @link-rp["day17-input.txt"]{input} is a list of containers that hold the designated number of liters. + + +@chunk[ + + + + ] + +@section{How many combinations of containers fit exactly 150 liters?} + +This is a lot like the second part of @racket{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 be 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[ + (require racket rackunit) + (provide (all-defined-out)) + + (define (powerset xs) + (if (empty? xs) + (list empty) + (append-map + (λ(s) (list (cons (car xs) s) s)) + (powerset (cdr xs))))) + ] + +@chunk[ + (define (q1 input-str) + (define containers + (map string->number (string-split input-str))) + (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[ + + (define (q2 input-str) + (define containers + (map string->number (string-split input-str))) + (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[ + (module+ test + (define input-str (file->string "day17-input.txt")) + (check-equal? (q1 input-str) 1638) + (check-equal? (q2 input-str) 17))] + +