diff options
author | HJ <thechairman@thechairman.info> | 2021-12-16 06:31:31 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2021-12-16 06:31:31 -0500 |
commit | 9fab205b1d55c34e9876accb819bf72f49cddabb (patch) | |
tree | 0e34f494355c8a0866e837dfc2274cbfc9d297b7 /2021/day-15 | |
parent | 3343d9c53d1cd112831fc74c01630352c2d5ee47 (diff) | |
download | gleam_aoc-9fab205b1d55c34e9876accb819bf72f49cddabb.tar.gz gleam_aoc-9fab205b1d55c34e9876accb819bf72f49cddabb.zip |
day 15 using structs; day 16 setup
Diffstat (limited to '2021/day-15')
-rw-r--r-- | 2021/day-15/day-15.rkt | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/2021/day-15/day-15.rkt b/2021/day-15/day-15.rkt index 38c558a..e9101a6 100644 --- a/2021/day-15/day-15.rkt +++ b/2021/day-15/day-15.rkt @@ -3,6 +3,8 @@ threading graph) +(struct Point (x y) #:transparent) + (define data (for/fold ([cells (hash)]) ([row (in-lines (open-day 15 2021))] @@ -11,18 +13,18 @@ ([n (in-string row)] [y (in-naturals)]) (hash-set cells - `(,x ,y) + (Point x y) (~> n string string->number))))) -(define x-max (~>> data hash-keys (map first) (apply max))) -(define y-max (~>> data hash-keys (map second) (apply max))) +(define x-max (~>> data hash-keys (map Point-x) (apply max))) +(define y-max (~>> data hash-keys (map Point-y) (apply max))) (define (neighbors pt d) - (match-define (list x y) pt) - (~>> (list (list (add1 x) y) - (list (sub1 x) y) - (list x (add1 y)) - (list x (sub1 y))) + (match-define (Point x y) pt) + (~>> (list (Point (add1 x) y) + (Point (sub1 x) y) + (Point x (add1 y)) + (Point x (sub1 y))) (filter (curry hash-has-key? d)))) (define (grid-graph d) @@ -35,10 +37,10 @@ ;; part 1 (define (find-path-weight d) (define grid (grid-graph d)) - (let-values ([(node-distances _) (dijkstra grid '(0 0))]) - (define xm (~>> d hash-keys (map first) (apply max))) - (define ym (~>> d hash-keys (map second) (apply max))) - (hash-ref node-distances (list xm ym)))) + (let-values ([(node-distances _) (dijkstra grid (Point 0 0))]) + (define xm (~>> d hash-keys (map Point-x) (apply max))) + (define ym (~>> d hash-keys (map Point-y) (apply max))) + (hash-ref node-distances (Point xm ym)))) (~> data find-path-weight @@ -51,14 +53,14 @@ (define (expand-data data) (for/fold ([cells (hash)]) ([coord (in-list (hash-keys data))]) - (match-define (list x y) coord) + (match-define (Point x y) coord) (for*/fold ([cells cells]) ([n (in-range 5)] [m (in-range 5)] [val (in-value (hash-ref data coord))]) (hash-set cells - (list (+ x (* n (add1 x-max))) - (+ y (* m (add1 y-max)))) + (Point (+ x (* n (add1 x-max))) + (+ y (* m (add1 y-max)))) (sequence-ref nine-cycle (+ val n m -1)))))) (~> data |