diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-12 00:51:10 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-12 00:51:10 -0500 |
commit | 69de3d8e95b8d66609e07dffcb2022dde901d837 (patch) | |
tree | 0721cc5a4bf5ba4943b771225e0f5ae895a9b09e /2022/day-12 | |
parent | d85b45a0945217b953ef89abcc5a7f45d1f73220 (diff) | |
download | gleam_aoc-69de3d8e95b8d66609e07dffcb2022dde901d837.tar.gz gleam_aoc-69de3d8e95b8d66609e07dffcb2022dde901d837.zip |
day 12 complete
Diffstat (limited to '2022/day-12')
-rw-r--r-- | 2022/day-12/day-12.rkt | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2022/day-12/day-12.rkt b/2022/day-12/day-12.rkt new file mode 100644 index 0000000..9120468 --- /dev/null +++ b/2022/day-12/day-12.rkt @@ -0,0 +1,45 @@ +#lang racket + +(require advent-of-code + fancy-app + graph) + +(define raw-terrain (fetch-aoc-input (find-session) 2022 12 #:cache #true)) +(define special-points (make-hash)) + +(define terrain-mesh + (for*/hash ([(row x) (in-indexed (string-split raw-terrain))] [(col y) (in-indexed row)]) + (case col + [(#\S) + (hash-set! special-points 'start (cons x y)) + (values (cons x y) 0)] + [(#\E) + (hash-set! special-points 'end (cons x y)) + (values (cons x y) 25)] + [else (values (cons x y) (- (char->integer col) (char->integer #\a)))]))) + +(define (neighbors p) + (match-define (cons x y) p) + (for*/list ([dx (in-list '(-1 0 1))] + [dy (in-list '(-1 0 1))] + #:when (= 1 (abs (+ dx dy))) + #:do [(define p (cons (+ x dx) (+ y dy)))] + #:when (hash-has-key? terrain-mesh p)) + p)) + +(define paths + (directed-graph (for*/list ([p (in-list (hash-keys terrain-mesh))] + [neighbor (in-list (neighbors p))] + #:unless (> (sub1 (hash-ref terrain-mesh neighbor)) + (hash-ref terrain-mesh p))) + (list p neighbor)))) + +;; part 1 +(match-define-values (distances _) (dijkstra paths (hash-ref special-points 'start))) +(hash-ref distances (hash-ref special-points 'end)) + +;; part 2 +(for/lists (lengths #:result (apply min lengths)) + ([start (in-list (hash-keys terrain-mesh))] #:when (= 0 (hash-ref terrain-mesh start))) + (match-define-values (distances _) (dijkstra paths start)) + (hash-ref distances (hash-ref special-points 'end))) |