diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2023-12-11 14:04:45 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2023-12-11 14:04:45 -0500 |
commit | e96d416f067467145626a6b46d96dd9b78d704d0 (patch) | |
tree | 960e279aae0e9b4dc6e07f34390a5fd89c7ef58b /aoc2023-other | |
parent | 0678dda364798282b6cb7ec80399c0d78f568e0c (diff) | |
download | gleam_aoc-e96d416f067467145626a6b46d96dd9b78d704d0.tar.gz gleam_aoc-e96d416f067467145626a6b46d96dd9b78d704d0.zip |
day 11 racket complete
Diffstat (limited to 'aoc2023-other')
-rw-r--r-- | aoc2023-other/day-11/day-11.rkt | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/aoc2023-other/day-11/day-11.rkt b/aoc2023-other/day-11/day-11.rkt new file mode 100644 index 0000000..883ae11 --- /dev/null +++ b/aoc2023-other/day-11/day-11.rkt @@ -0,0 +1,36 @@ +#lang racket + +(require advent-of-code + threading) + +(struct posn (x y) #:transparent) + +(define input + (~> (fetch-aoc-input (find-session) 2023 11 #:cache #true) + (string-split "\n") + (map string->list _))) + +(define (get-empty-ranks grid) + (for/list ([(rank n) (in-indexed grid)] #:when (equal? '(#\.) (remove-duplicates rank))) + n)) + +(define (count-prior-empty-ranks rank empty-ranks) + (~> empty-ranks (takef (curryr < rank)) length)) + +(define empty-rows (get-empty-ranks input)) +(define empty-columns (get-empty-ranks (apply map list input))) ;; transpose + +(define (sum-of-star-distances in expand-by) + (define stars + (for*/list ([(row x) (in-indexed in)] [(col y) (in-indexed row)] #:when (equal? col #\#)) + (posn (+ x (* (sub1 expand-by) (count-prior-empty-ranks x empty-rows))) + (+ y (* (sub1 expand-by) (count-prior-empty-ranks y empty-columns)))))) + (for/sum ([star-pair (in-combinations stars 2)]) + (match-define (list (posn x1 y1) (posn x2 y2)) star-pair) + (+ (abs (- x1 x2)) (abs (- y1 y2))))) + +;; part 1 +(sum-of-star-distances input 2) + +;; part 2 +(sum-of-star-distances input 1000000) |