aboutsummaryrefslogtreecommitdiff
path: root/2022/day-13/day-13.rkt
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-14 10:47:16 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-14 10:47:16 -0500
commitef1c9034a0269156b0b0e99cd1ff14192c24e0ef (patch)
tree8cea7440022c4488114b91858bddf2b47b9c40be /2022/day-13/day-13.rkt
parent1765de8f51c0041c38423715308e4e6cec556564 (diff)
parent520ac0b83f5264fc708ecaf09980d60eb1ce4b2f (diff)
downloadgleam_aoc-ef1c9034a0269156b0b0e99cd1ff14192c24e0ef.tar.gz
gleam_aoc-ef1c9034a0269156b0b0e99cd1ff14192c24e0ef.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to '2022/day-13/day-13.rkt')
-rw-r--r--2022/day-13/day-13.rkt28
1 files changed, 28 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..d4e3185
--- /dev/null
+++ b/2022/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* 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)