From 8b4300ef235cc6bcb812fcf1226aadb01edecd7b Mon Sep 17 00:00:00 2001 From: Hunky Jimpjorps Date: Wed, 14 Dec 2022 10:47:07 -0500 Subject: day 12 tweaks --- 2022/day-12/day-12.rkt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to '2022') diff --git a/2022/day-12/day-12.rkt b/2022/day-12/day-12.rkt index 5e0c365..ed93453 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,14 +8,15 @@ (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) -- cgit v1.2.3 From 1765de8f51c0041c38423715308e4e6cec556564 Mon Sep 17 00:00:00 2001 From: Hunky Jimpjorps Date: Wed, 14 Dec 2022 10:47:10 -0500 Subject: day 12 tweaks --- 2022/day-12/day-12.rkt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to '2022') diff --git a/2022/day-12/day-12.rkt b/2022/day-12/day-12.rkt index ed93453..c3f01ac 100644 --- a/2022/day-12/day-12.rkt +++ b/2022/day-12/day-12.rkt @@ -23,16 +23,16 @@ (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))) -- cgit v1.2.3 From 9384f349e3fe5d422141bf64be05e7ae477c99f0 Mon Sep 17 00:00:00 2001 From: Hunky Jimpjorps Date: Wed, 14 Dec 2022 10:49:26 -0500 Subject: day 13 tweaks --- 2022/day-13/day-13.rkt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to '2022') 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) -- cgit v1.2.3 From 35e350d736d73dbbfe2505d3fc3bda7e7ca120ea Mon Sep 17 00:00:00 2001 From: Hunky Jimpjorps Date: Wed, 14 Dec 2022 12:46:34 -0500 Subject: day 14 tweaks --- 2022/day-14/day-14.rkt | 75 ++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 45 deletions(-) (limited to '2022') 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))) -- cgit v1.2.3