aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-15 10:25:23 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-15 10:25:23 -0500
commitc7388c9aa7fd7777e637cbb14bf040bdb0b8e8d8 (patch)
tree44911e1fb38de3bca0645e316f1849d79e4467f7 /2022
parentc4389f82c7c01e9d2beb3d38963277361ebe0cba (diff)
parent35e350d736d73dbbfe2505d3fc3bda7e7ca120ea (diff)
downloadgleam_aoc-c7388c9aa7fd7777e637cbb14bf040bdb0b8e8d8.tar.gz
gleam_aoc-c7388c9aa7fd7777e637cbb14bf040bdb0b8e8d8.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode into main
Diffstat (limited to '2022')
-rw-r--r--2022/day-12/day-12.rkt24
-rw-r--r--2022/day-13/day-13.rkt6
-rw-r--r--2022/day-14/day-14.rkt75
3 files changed, 45 insertions, 60 deletions
diff --git a/2022/day-12/day-12.rkt b/2022/day-12/day-12.rkt
index 5e0c365..c3f01ac 100644
--- a/2022/day-12/day-12.rkt
+++ b/2022/day-12/day-12.rkt
@@ -1,7 +1,6 @@
#lang racket
(require advent-of-code
- fancy-app
graph)
(define raw-terrain (fetch-aoc-input (find-session) 2022 12 #:cache #true))
@@ -9,30 +8,31 @@
(define terrain-mesh
(for*/hash ([(row x) (in-indexed (string-split raw-terrain))] [(col y) (in-indexed row)])
+ (define p (cons x y))
(case col
[(#\S)
- (hash-set! special-points 'start (cons x y))
- (values (cons x y) 0)]
+ (hash-set! special-points 'start p)
+ (values p 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)))])))
+ (hash-set! special-points 'end p)
+ (values p 25)]
+ [else (values p (- (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))
+ #: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))
+ [p* (in-list (neighbors p))]
+ #:unless (> (sub1 (hash-ref terrain-mesh p*))
(hash-ref terrain-mesh p)))
- (list p neighbor))))
+ (list p p*))))
;; part 1
(time (match-define-values (distances _) (bfs paths (hash-ref special-points 'start)))
diff --git a/2022/day-13/day-13.rkt b/2022/day-13/day-13.rkt
index d4e3185..39435e9 100644
--- a/2022/day-13/day-13.rkt
+++ b/2022/day-13/day-13.rkt
@@ -10,11 +10,11 @@
(match* (xs ys)
[('() (list* _)) #true]
[((list* _) '()) #false]
- [((list* a x-rest) (list* a y-rest)) (compare x-rest y-rest)]
+ [((list* _same x-rest) (list* _same y-rest)) (compare x-rest y-rest)]
[((list* (? integer? x) _) (list* (? integer? y) _)) (< x y)]
[((list* (? list? xs*) _) (list* (? list? ys*) _)) (compare xs* ys*)]
- [((list* (? list?) _) (list* (? integer? y) y-rest)) (compare xs (cons (list y) y-rest))]
- [((list* (? integer? x) x-rest) (list* (? list?) _)) (compare (cons (list x) x-rest) ys)]))
+ [(xs (list* (? integer? y) y-rest)) (compare xs (cons (list y) y-rest))]
+ [((list* (? integer? x) x-rest) ys) (compare (cons (list x) x-rest) ys)]))
;; part 1
(for/sum ([i (in-naturals 1)] [packet (in-slice 2 raw-packets)] #:when (apply compare packet)) i)
diff --git a/2022/day-14/day-14.rkt b/2022/day-14/day-14.rkt
index 7950339..c6b5c88 100644
--- a/2022/day-14/day-14.rkt
+++ b/2022/day-14/day-14.rkt
@@ -8,57 +8,42 @@
(define (trace-line-between-points p1 p2)
(match* (p1 p2)
- [((list x y1) (list x y2))
- (for/list ([y [in-inclusive-range (min y1 y2) (max y1 y2)]])
- (cons x y))]
- [((list x1 y) (list x2 y))
- (for/list ([x [in-inclusive-range (min x1 x2) (max x1 x2)]])
- (cons x y))]))
+ [((list x y1) (list x y2)) (map (λ (y) (cons x y)) (inclusive-range (min y1 y2) (max y1 y2)))]
+ [((list x1 y) (list x2 y)) (map (λ (x) (cons x y)) (inclusive-range (min x1 x2) (max x1 x2)))]))
-(define all-coordinates
- (apply append
- (for/list ([formation (in-list (string-split data "\n"))])
- (define coord-list
- (for/list ([coord-pair (in-list (string-split formation " -> "))])
- (for/list ([coord (in-list (string-split coord-pair ","))])
- (string->number coord))))
- (apply append (adjacent-map trace-line-between-points coord-list)))))
+(define (find-points-in-structure str)
+ (define endpoints
+ (for/list ([coord-pair (in-list (string-split str " -> "))])
+ (for/list ([coord (in-list (string-split coord-pair ","))])
+ (string->number coord))))
+ (~>> endpoints (adjacent-map trace-line-between-points) (apply append) (list->set)))
-(define rock-structures-hash
- (for/hash ([p (in-list all-coordinates)])
- (values p 'rock)))
+(define blocked-locations
+ (~> data (string-split "\n") (map find-points-in-structure _) (apply set-union _)))
-(define max-vertical-distance (~>> all-coordinates (argmax cdr) cdr add1))
+(define max-vertical-distance (~>> blocked-locations (set->list) (argmax cdr) cdr add1))
-(define (open? h x y)
- (not (hash-has-key? h (cons x y))))
+(define (open? pts p)
+ (not (set-member? pts p)))
;; part 1
-(define (trace-grain h [pos (cons 500 0)])
- (match-define (cons x y) pos)
+(define (trace-grain pts [pt (cons 500 0)] #:at-limit do-at-limit)
+ (match-define (cons x y) pt)
(cond
- [(> y max-vertical-distance) 'break]
- [(open? h x (add1 y)) (trace-grain h (cons x (add1 y)))]
- [(open? h (sub1 x) (add1 y)) (trace-grain h (cons (sub1 x) (add1 y)))]
- [(open? h (add1 x) (add1 y)) (trace-grain h (cons (add1 x) (add1 y)))]
- [else (hash-set h (cons x y) 'sand)]))
-
-(for/fold ([h rock-structures-hash] [grains 0] #:result grains) ([_ (in-naturals 1)])
- (define h* (trace-grain h))
- #:break (eq? h* 'break)
- (values h* (add1 grains)))
+ [(>= y max-vertical-distance) (do-at-limit pts pt)]
+ [(ormap (λ (d)
+ (let ([p* (cons (+ x d) (add1 y))])
+ (if (open? pts p*) (trace-grain pts p* #:at-limit do-at-limit) #f)))
+ '(0 -1 1))]
+ [else (set-add pts pt)]))
+
+(for/fold ([pts blocked-locations] [grains 0] #:result grains) ([_ (in-naturals 1)])
+ (define pts* (trace-grain pts #:at-limit (const 'break)))
+ #:break (equal? pts* 'break)
+ (values pts* (add1 grains)))
;; part 2
-(define (trace-grain* h [pos (cons 500 0)])
- (match-define (cons x y) pos)
- (cond
- [(= y max-vertical-distance) (hash-set h (cons x y) 'sand)]
- [(open? h x (add1 y)) (trace-grain* h (cons x (add1 y)))]
- [(open? h (sub1 x) (add1 y)) (trace-grain* h (cons (sub1 x) (add1 y)))]
- [(open? h (add1 x) (add1 y)) (trace-grain* h (cons (add1 x) (add1 y)))]
- [else (hash-set h (cons x y) 'sand)]))
-
-(for/fold ([h rock-structures-hash] [grains 0] #:result grains) ([_ (in-naturals 1)])
- #:break (not (open? h 500 0))
- (define h* (trace-grain* h))
- (values h* (add1 grains)))
+(for/fold ([pts blocked-locations] [grains 0] #:result grains) ([_ (in-naturals 1)])
+ #:break (not (open? pts (cons 500 0)))
+ (define pts* (trace-grain pts #:at-limit set-add))
+ (values pts* (add1 grains)))