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 | |
parent | 3343d9c53d1cd112831fc74c01630352c2d5ee47 (diff) | |
download | gleam_aoc-9fab205b1d55c34e9876accb819bf72f49cddabb.tar.gz gleam_aoc-9fab205b1d55c34e9876accb819bf72f49cddabb.zip |
day 15 using structs; day 16 setup
-rw-r--r-- | 2021/day-15/day-15.rkt | 32 | ||||
-rw-r--r-- | 2021/day-16/day-16.rkt | 30 |
2 files changed, 47 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 diff --git a/2021/day-16/day-16.rkt b/2021/day-16/day-16.rkt new file mode 100644 index 0000000..80c2a54 --- /dev/null +++ b/2021/day-16/day-16.rkt @@ -0,0 +1,30 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(define data + (~> (open-day 16 2021) + port->string + string-trim + string->list)) + +(define (hex->bin h) + (match h + [#\0 '(0 0 0 0)] + [#\1 '(0 0 0 1)] + [#\2 '(0 0 1 0)] + [#\3 '(0 0 1 1)] + [#\4 '(0 1 0 0)] + [#\5 '(0 1 0 1)] + [#\6 '(0 1 1 0)] + [#\7 '(0 1 1 1)] + [#\8 '(1 0 0 0)] + [#\9 '(1 0 0 1)] + [#\A '(1 0 1 0)] + [#\B '(1 0 1 1)] + [#\C '(1 1 0 0)] + [#\D '(1 1 0 1)] + [#\E '(1 1 1 0)] + [#\F '(1 1 1 1)])) + +(map hex->bin data)
\ No newline at end of file |