From 6f797ccca21cef7a8aadf6ef9b8fcda88eb025bc Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 11 Dec 2021 22:56:51 -0800 Subject: [PATCH] Day 12. --- input/12.txt | 21 +++++++++++++++++++++ src/12.rkt | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 input/12.txt create mode 100644 src/12.rkt diff --git a/input/12.txt b/input/12.txt new file mode 100644 index 0000000..6e568ac --- /dev/null +++ b/input/12.txt @@ -0,0 +1,21 @@ +ma-start +YZ-rv +MP-rv +vc-MP +QD-kj +rv-kj +ma-rv +YZ-zd +UB-rv +MP-xe +start-MP +zd-end +ma-UB +ma-MP +UB-xe +end-UB +ju-MP +ma-xe +zd-UB +start-xe +YZ-end diff --git a/src/12.rkt b/src/12.rkt new file mode 100644 index 0000000..13dec2e --- /dev/null +++ b/src/12.rkt @@ -0,0 +1,41 @@ +#lang curly-fn racket + +(require racket/set + "../lib.rkt") + +(define input + (for/fold ([caves (hash)]) + ([connection (problem-input 12)]) + (match-let* ([(list cave1 cave2) (string-split connection "-")] + [caves (hash-update caves cave1 #{set-add % cave2} (set))] + [caves (hash-update caves cave2 #{set-add % cave1} (set))]) + caves))) + +(struct path+ (path small-caves twice?)) + +(define (small? cave) + (char-lower-case? (string-ref cave 0))) + +(define (get-paths once?) + (let loop ([queue (list (path+ '("start") (set) once?))] + [paths 0]) + (match queue + ['() paths] + [(list (path+ path small-caves twice?) queue ...) + (define adjacent + (hash-ref input (first path))) + (define visitable + (if twice? + (set-subtract adjacent (set "start" "end") small-caves) + (set-subtract adjacent (set "start" "end")))) + (define queue* + (for/list ([cave visitable]) + (path+ (cons cave path) + (if (small? cave) (set-add small-caves cave) small-caves) + (or (set-member? small-caves cave) twice?)))) + (define paths* + (if (set-member? adjacent "end") + (add1 paths) paths)) + (loop (append queue* queue) paths*)]))) + +(show-solution (get-paths #t) (get-paths #f)) \ No newline at end of file