This turned out to be the easiest pair of puzzles to date. Maybe I’m getting better at this.
The puzzle tells us that for a triangle to be valid, the sum of any two sides must be greater than the remaining side. If you think about it for a bit, you’ll see that the two smallest sides are the only ones you need to add.
There’s nothing left to do except convert each line of the input into a triangle—
Racket’s count function counts values list that pass a given set of criteria. The first argument is the name of the function we want to use to tell it which values to count: if that function returns anything besides #f for a given value, that value is included in the count. And since (identity x) simply returns x, passing identity to count amounts to saying, "count all the non-false values in this list".
(define (string->triangles str) (for/list [(ts (in-list (map string-split (string-split str "\n"))))] (map string->number ts))) (define (q1 str) (count identity (map triangle-valid? (string->triangles str))))
You’ve got to be kidding me. You’re asking me to rearrange a list of lists into a new list of lists, in a language where lists are basically everything?
Please, don’t throw me in that briar patch!
Ok, so: all we need is a new string->triangles function that looks at the lines three at a time, and pulls from the first, second, and third columns of each. It was in the course of writing this function that I learned of Racket’s for*/list function: it works exactly like nested for loops work in other languages, but without the redundancy.
(define (q2-string->triangles str) (define lines (map string-split (string-split str "\n"))) (for*/list ([row (in-range 0 (length lines) 3)] [col (in-range 3)]) (map string->number (list (list-ref (list-ref lines row) col) (list-ref (list-ref lines (+ row 1)) col) (list-ref (list-ref lines (+ row 2)) col))))) (define (q2 str) (count identity (map triangle-valid? (q2-string->triangles str)))) (define q2-test-string (string-append "101 301 501\n" "102 302 502\n" "103 303 503\n" "201 401 601\n" "202 402 602\n" "203 403 603"))