diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-13 01:18:44 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-13 01:18:44 -0500 |
commit | 80fbd3beb20303dc5be6272a6e6e14939d5d94d0 (patch) | |
tree | a9b2a73242344c913ba00f5e98cf905b3f6da7f7 | |
parent | 69de3d8e95b8d66609e07dffcb2022dde901d837 (diff) | |
download | gleam_aoc-80fbd3beb20303dc5be6272a6e6e14939d5d94d0.tar.gz gleam_aoc-80fbd3beb20303dc5be6272a6e6e14939d5d94d0.zip |
day 13 complete
-rw-r--r-- | 2022/day-13/day-13.rkt | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2022/day-13/day-13.rkt b/2022/day-13/day-13.rkt new file mode 100644 index 0000000..fbba69a --- /dev/null +++ b/2022/day-13/day-13.rkt @@ -0,0 +1,31 @@ +#lang racket + +(require advent-of-code + threading) + +(define raw-packets + (~> (fetch-aoc-input (find-session) 2022 13 #:cache #true) + (string-replace _ "," " ") + (string-split "\n" #:repeat? #true) + (map (λ (str) (apply append (port->list read (open-input-string str)))) _))) + +(define (compare xs ys) + (match* (xs ys) + [('() (list* _)) #true] + [((list* _) '()) #false] + [((list* a x-rest) (list* a y-rest)) (compare x-rest y-rest)] + [((list* (? integer? x) _) (list* (? integer? y) _)) (< x y)] + [((list* (? list? xs*) _) (list* (? list? ys*) _)) (compare xs* ys*)] + [((list* (? list?) _) (list* (? integer? y) y-rest)) (compare xs (cons (list y) y-rest))] + [((list* (? integer? x) x-rest) (list* (? list?) _)) (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) |