aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2022/day-17
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/aoc2022/day-17
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'racket/aoc2022/day-17')
-rw-r--r--racket/aoc2022/day-17/day-17.rkt53
-rw-r--r--racket/aoc2022/day-17/rock-shapes17
2 files changed, 70 insertions, 0 deletions
diff --git a/racket/aoc2022/day-17/day-17.rkt b/racket/aoc2022/day-17/day-17.rkt
new file mode 100644
index 0000000..28e8763
--- /dev/null
+++ b/racket/aoc2022/day-17/day-17.rkt
@@ -0,0 +1,53 @@
+#lang racket
+
+(require advent-of-code
+ threading
+ fancy-app
+ data/collection)
+
+(define (relative-rock-coordinates rock)
+ (for*/hash ([(row y) (in-indexed (reverse rock))] [(_col x) (in-indexed row)])
+ (values (cons x y) #true)))
+
+(define rock-shapes
+ (~> (open-input-file "./2022/day-17/rock-shapes")
+ port->string
+ (string-split "\n\n")
+ (map (λ~> (string-split _ "\n") (map string->list _) relative-rock-coordinates) _)))
+
+(define gusts
+ (~> (fetch-aoc-input (find-session) 2022 17 #:cache #true)
+ string-trim
+ string->list
+ (map (match-lambda
+ [#\> 1]
+ [#\< -1])
+ _)))
+
+(define (place-new-rock rock elevation)
+ (for/hash ([(posn _) (in-hash rock)])
+ (match-define (cons x y) posn)
+ (values (cons (+ x 3) (+ y elevation 4)) #true)))
+
+(define (gust-move rock wind cave)
+ (define moved-rock
+ (for/hash ([(posn _) (in-hash rock)])
+ (match-define (cons x y) posn)
+ (define posn* (cons (+ x wind) y))
+ (if (hash-has-key? cave posn) (values 'collision #t) (values posn* #t))))
+ (if (hash-has-key? moved-rock 'collision) rock moved-rock))
+
+(for/fold ([cave (hash)]
+ [falling-rock #f]
+ [top-of-rocks 0]
+ [rock-cycle (cycle rock-shapes)]
+ [rock-number 0]
+ #:result top-of-rocks)
+ (#:break (> rock-number 2022) [gust (in-cycle gusts)])
+ (cond
+ [(not falling-rock)
+ (values cave
+ (~> (first rock-cycle) (place-new-rock top-of-rocks) (gust-move gust cave))
+ top-of-rocks
+ (rest rock-cycle)
+ (add1 rock-number))]))
diff --git a/racket/aoc2022/day-17/rock-shapes b/racket/aoc2022/day-17/rock-shapes
new file mode 100644
index 0000000..fbcc382
--- /dev/null
+++ b/racket/aoc2022/day-17/rock-shapes
@@ -0,0 +1,17 @@
+####
+
+.#.
+###
+.#.
+
+..#
+..#
+###
+
+#
+#
+#
+#
+
+##
+## \ No newline at end of file