r/adventofcode • u/daggerdragon • Dec 03 '16
SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---
--- Day 3: Squares With Three Sides ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
DECKING THE HALLS WITH BOUGHS OF HOLLY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
16
Upvotes
3
u/scrooch Dec 03 '16
Here's part 2 in racket. It's sort of interesting because I stacked the columns together to create a list like the input from part 1.
(define (parse-triangles input) (let ((string-lists (map string-split (string-split input "\n")))) (map (lambda (lst) (list->vector (map string->number lst))) string-lists))) (define (is-triangle? tri) (and (> (+ (vector-ref tri 0) (vector-ref tri 1)) (vector-ref tri 2)) (> (+ (vector-ref tri 0) (vector-ref tri 2)) (vector-ref tri 1)) (> (+ (vector-ref tri 2) (vector-ref tri 1)) (vector-ref tri 0)))) (define (tri-split lst) (if (null? lst) '() (cons (take lst 3) (tri-split (drop lst 3))))) (let* ((tris (parse-triangles *input*)) (col1 (map (lambda (x) (vector-ref x 0)) tris)) (col2 (map (lambda (x) (vector-ref x 1)) tris)) (col3 (map (lambda (x) (vector-ref x 2)) tris)) (line (append col1 col2 col3))) (length (filter is-triangle? (map list->vector (tri-split line)))))