diff options
Diffstat (limited to 'racket/aoc2022/day-13')
-rw-r--r-- | racket/aoc2022/day-13/day-13.rkt | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/racket/aoc2022/day-13/day-13.rkt b/racket/aoc2022/day-13/day-13.rkt new file mode 100644 index 0000000..39435e9 --- /dev/null +++ b/racket/aoc2022/day-13/day-13.rkt @@ -0,0 +1,28 @@ +#lang racket + +(require advent-of-code) + +(define raw-packets + (parameterize ([current-readtable (make-readtable #f #\, #\space #f)]) + (port->list read (open-aoc-input (find-session) 2022 13 #:cache #true)))) + +(define (compare xs ys) + (match* (xs ys) + [('() (list* _)) #true] + [((list* _) '()) #false] + [((list* _same x-rest) (list* _same y-rest)) (compare x-rest y-rest)] + [((list* (? integer? x) _) (list* (? integer? y) _)) (< x y)] + [((list* (? list? xs*) _) (list* (? list? ys*) _)) (compare xs* ys*)] + [(xs (list* (? integer? y) y-rest)) (compare xs (cons (list y) y-rest))] + [((list* (? integer? x) x-rest) ys) (compare (cons (list x) x-rest) ys)])) + +;; part 1 +(for/sum ([i (in-naturals 1)] [packet (in-slice 2 raw-packets)] #:when (apply compare packet)) i) + +;; part 2 +(define divider-packets (list '((2)) '((6)))) +(define amended-packets (append divider-packets raw-packets)) + +(for/product ([i (in-naturals 1)] [packet (in-list (sort amended-packets compare))] + #:when (member packet divider-packets)) + i) |