diff options
author | HJ <thechairman@thechairman.info> | 2023-12-25 11:24:39 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2023-12-25 11:24:39 -0500 |
commit | 40eb8465f7958ac056a3843d38234848b15464f7 (patch) | |
tree | 6a22b862107f219c6b1c354c4ac70792e9d41ce8 /aoc2023-other | |
parent | f3dfb53b1d59febe1f3bac746150372362f313a9 (diff) | |
download | gleam_aoc-40eb8465f7958ac056a3843d38234848b15464f7.tar.gz gleam_aoc-40eb8465f7958ac056a3843d38234848b15464f7.zip |
day 1-24 end-of-year style cleanup
Diffstat (limited to 'aoc2023-other')
-rw-r--r-- | aoc2023-other/day-03/day-03.rkt | 22 | ||||
-rw-r--r-- | aoc2023-other/day-04/day-04.rkt | 2 | ||||
-rw-r--r-- | aoc2023-other/day-05/day-05.rkt | 4 | ||||
-rw-r--r-- | aoc2023-other/day-06/day-06.rkt | 6 | ||||
-rw-r--r-- | aoc2023-other/day-07/day-07.rkt | 43 | ||||
-rw-r--r-- | aoc2023-other/day-08/day-08.rkt | 10 | ||||
-rw-r--r-- | aoc2023-other/day-09/day-09-polynomial.rkt | 10 | ||||
-rw-r--r-- | aoc2023-other/day-09/day-09.rkt | 20 | ||||
-rw-r--r-- | aoc2023-other/day-10/day-10.rkt | 11 | ||||
-rw-r--r-- | aoc2023-other/day-11/day-11.rkt | 8 | ||||
-rw-r--r-- | aoc2023-other/day-12/day-12.rkt | 69 | ||||
-rw-r--r-- | aoc2023-other/day-14/day-14.rkt | 12 | ||||
-rw-r--r-- | aoc2023-other/day-16/day-16.rkt | 2 | ||||
-rw-r--r-- | aoc2023-other/day-17/day-17.rkt | 11 | ||||
-rw-r--r-- | aoc2023-other/day-18/day-18.rkt | 12 | ||||
-rw-r--r-- | aoc2023-other/day-19/day-19.rkt | 8 | ||||
-rw-r--r-- | aoc2023-other/day-20/day-20.rkt | 40 | ||||
-rw-r--r-- | aoc2023-other/day-21/day-21.rkt | 4 | ||||
-rw-r--r-- | aoc2023-other/day-22/day-22.rkt | 4 | ||||
-rw-r--r-- | aoc2023-other/day-23/day-23.rkt | 11 | ||||
-rw-r--r-- | aoc2023-other/day-24/day-24a.rkt | 20 | ||||
-rw-r--r-- | aoc2023-other/day-24/day-24b.rkt | 6 |
22 files changed, 205 insertions, 130 deletions
diff --git a/aoc2023-other/day-03/day-03.rkt b/aoc2023-other/day-03/day-03.rkt index d52b11b..60e81a6 100644 --- a/aoc2023-other/day-03/day-03.rkt +++ b/aoc2023-other/day-03/day-03.rkt @@ -31,23 +31,28 @@ (define (group-into-parts cells [acc '()]) (match* (cells acc) - [('() acc) acc] - [((list* (cons (and pt (posn x y)) n) cs) (list* (part n* (and pts (list* (posn x* y) rest-pts))) - rest-acc)) + [('() acc) + acc] + [((list* (cons (and pt (posn x y)) n) cs) + (list* (part n* (and pts (list* (posn x* y) rest-pts))) + rest-acc)) #:when (= (- x x*) 1) (group-into-parts cs (cons (part (+ n (* n* 10)) (cons pt pts)) rest-acc))] - [((list* (cons pt n) cs) acc) (group-into-parts cs (cons (part n (list pt)) acc))])) + [((list* (cons pt n) cs) acc) + (group-into-parts cs (cons (part n (list pt)) acc))])) (define (neighbors p) - (for*/list ([dx '(-1 0 1)] [dy '(-1 0 1)] #:unless (and (= dx 0) (= dy 0))) + (for*/list ([dx '(-1 0 1)] + [dy '(-1 0 1)] + #:unless (and (= dx 0) (= dy 0))) (posn (+ dx (posn-x p)) (+ dy (posn-y p))))) (define to-neighbors (λ~>> part-posns (append-map neighbors) remove-duplicates)) (define (symbol-in-neighbors b pt acc) (~>> pt to-neighbors - (ormap (λ (p) - (let ([lookup (hash-ref b p #f)]) (or (equal? lookup 'gear) (equal? lookup 'other))))) + (ormap (λ (p) (let ([lookup (hash-ref b p #f)]) + (or (equal? lookup 'gear) (equal? lookup 'other))))) ((λ (bool) (if bool (+ acc (part-n pt)) acc))))) ;; part 1 @@ -56,8 +61,7 @@ ;; part 2 (define gears (~>> board (find-cells (curry equal? 'gear)) (map car))) -(define parts-with-neighbors - (~>> parts (map (λ (pt) (struct-copy part pt [posns (to-neighbors pt)]))))) +(define parts-with-neighbors (map (λ (pt) (struct-copy part pt [posns (to-neighbors pt)])) parts)) (define (find-parts-near-gear pts gear) (filter-map (λ (pt) (and (findf (curry equal? gear) (part-posns pt)) (part-n pt))) pts)) diff --git a/aoc2023-other/day-04/day-04.rkt b/aoc2023-other/day-04/day-04.rkt index 2b4c0b0..7a357c5 100644 --- a/aoc2023-other/day-04/day-04.rkt +++ b/aoc2023-other/day-04/day-04.rkt @@ -8,7 +8,7 @@ megaparsack/text threading) -(struct card (n wins) #:transparent) +(struct card (n wins)) (define card/p (do (string/p "Card") diff --git a/aoc2023-other/day-05/day-05.rkt b/aoc2023-other/day-05/day-05.rkt index 0373619..5b9aa52 100644 --- a/aoc2023-other/day-05/day-05.rkt +++ b/aoc2023-other/day-05/day-05.rkt @@ -4,8 +4,8 @@ algorithms threading) -(struct map-range (start end offset) #:transparent) -(struct seed-range (start end) #:transparent) +(struct map-range (start end offset)) +(struct seed-range (start end)) (define input (fetch-aoc-input (find-session) 2023 5 #:cache #true)) diff --git a/aoc2023-other/day-06/day-06.rkt b/aoc2023-other/day-06/day-06.rkt index 399c131..53ca9ee 100644 --- a/aoc2023-other/day-06/day-06.rkt +++ b/aoc2023-other/day-06/day-06.rkt @@ -1,8 +1,7 @@ #lang racket (require advent-of-code - threading - ) + threading) (match-define (list times distances) (~> (open-aoc-input (find-session) 2023 6 #:cache #true) port->lines)) @@ -20,7 +19,8 @@ (find-bound rtime dist rtime -1)) (for/fold ([acc 1]) - ([race-time (in-list (get-numbers times))] [distance (in-list (get-numbers distances))]) + ([race-time (in-list (get-numbers times))] + [distance (in-list (get-numbers distances))]) (* acc (add1 (- (upper-bound race-time distance) (lower-bound race-time distance))))) ;; part 2 diff --git a/aoc2023-other/day-07/day-07.rkt b/aoc2023-other/day-07/day-07.rkt index f0a40ea..30e629b 100644 --- a/aoc2023-other/day-07/day-07.rkt +++ b/aoc2023-other/day-07/day-07.rkt @@ -4,7 +4,7 @@ threading memo) -(struct hand (cards wager) #:transparent) +(struct hand (cards wager)) (define/match (card->int card) [((? char-numeric?)) (~> card string string->number)] @@ -28,16 +28,16 @@ (define input (~> (open-aoc-input (find-session) 2023 7 #:cache #true) port->lines)) (define/memoize (identify-hand h) - (define freqs (~> h hand-cards (sort <) (group-by identity _) (map length _))) - (match freqs - [(list-no-order 5) 8] - [(list-no-order 1 4) 7] - [(list-no-order 2 3) 6] - [(list-no-order 1 1 3) 5] - [(list-no-order 1 2 2) 4] - [(list-no-order 1 1 1 2) 3] - [(list-no-order 1 1 1 1 1) 2] - [_ 1])) + (define freqs (~> h hand-cards (sort <) (group-by identity _) (map length _))) + (match freqs + [(list-no-order 5) 8] + [(list-no-order 1 4) 7] + [(list-no-order 2 3) 6] + [(list-no-order 1 1 3) 5] + [(list-no-order 1 2 2) 4] + [(list-no-order 1 1 1 2) 3] + [(list-no-order 1 1 1 1 1) 2] + [_ 1])) (define (compare-first-card cs1 cs2) (if (= (first cs1) (first cs2)) @@ -55,21 +55,26 @@ (compare-hands identify-hand h1 h2)) (define (total-score in #:jokers [jokers? #false]) + (define sorted-hands + (~> in + (map (curry parse-hand #:jokers jokers?) _) + (sort (if jokers? compare-hands-no-wilds compare-hands-with-wilds)))) (for/sum ([(h i) - (in-indexed (~> in - (map (curry parse-hand #:jokers jokers?) _) - (sort (if jokers? compare-hands-no-wilds compare-hands-with-wilds))))]) - (* (add1 i) (hand-wager h)))) + (in-indexed sorted-hands)]) + (* (add1 i) (hand-wager h)))) (total-score input) ;; part 2 (define/memoize (find-best-joker-substitution h) - (for/fold ([best-hand (hand '() 0)]) ([wild (in-inclusive-range 2 14)]) - (define trial-hand - (hand (map (λ (c) (if (= c 1) wild c)) (hand-cards h)) (hand-wager h))) - (if (> (identify-hand trial-hand) (identify-hand best-hand)) trial-hand best-hand))) + (for/fold ([best-hand (hand '() 0)]) + ([wild (in-inclusive-range 2 14)]) + (define trial-hand + (hand (map (λ (c) (if (= c 1) wild c)) (hand-cards h)) (hand-wager h))) + (if (> (identify-hand trial-hand) (identify-hand best-hand)) + trial-hand + best-hand))) (define (compare-hands-with-wilds h1 h2) (compare-hands (λ~> find-best-joker-substitution identify-hand) h1 h2)) diff --git a/aoc2023-other/day-08/day-08.rkt b/aoc2023-other/day-08/day-08.rkt index bc234b5..06daafa 100644 --- a/aoc2023-other/day-08/day-08.rkt +++ b/aoc2023-other/day-08/day-08.rkt @@ -16,7 +16,10 @@ [(list _ name left right) (values name (exits left right))]))) (define (to-next-node start end dirs maze) - (for/fold ([current start] [acc 0] #:result acc) ([dir (in-cycle dirs)]) + (for/fold ([current start] + [acc 0] + #:result acc) + ([dir (in-cycle dirs)]) #:break (string-suffix? current end) (define node (hash-ref maze current)) (case dir @@ -28,5 +31,6 @@ ;; part 2 (for/lists (ns #:result (apply lcm ns)) - ([start (in-list (hash-keys maze))] #:when (string-suffix? start "A")) - (to-next-node start "Z" directions maze)) + ([start (in-list (hash-keys maze))] + #:when (string-suffix? start "A")) + (to-next-node start "Z" directions maze)) diff --git a/aoc2023-other/day-09/day-09-polynomial.rkt b/aoc2023-other/day-09/day-09-polynomial.rkt index 31c0d3d..5bacb1f 100644 --- a/aoc2023-other/day-09/day-09-polynomial.rkt +++ b/aoc2023-other/day-09/day-09-polynomial.rkt @@ -6,8 +6,12 @@ (define histories (for/list ([raw-history (in-lines (open-aoc-input (find-session) 2023 9 #:cache #true))]) - (~>> raw-history string-split (map string->number)))) + (~>> raw-history + string-split + (map string->number)))) -(for/lists (left right #:result (cons (apply + left) (apply + right)))([history (in-list histories)]) +(for/lists (left right #:result (cons (apply + left) (apply + right))) + ([history (in-list histories)]) (define f (interpolate-at-integer-points history)) - (values (f -1) (f (length history)))) + (values (f -1) + (f (length history)))) diff --git a/aoc2023-other/day-09/day-09.rkt b/aoc2023-other/day-09/day-09.rkt index c632150..5eda1eb 100644 --- a/aoc2023-other/day-09/day-09.rkt +++ b/aoc2023-other/day-09/day-09.rkt @@ -5,18 +5,28 @@ (define histories (for/list ([raw-history (in-lines (open-aoc-input (find-session) 2023 9 #:cache #true))]) - (~>> raw-history string-split (map string->number)))) + (~>> raw-history + string-split + (map string->number)))) -(define (constant? xs) (= 1 (length (remove-duplicates xs)))) +(define (constant? xs) + (= 1 (length (remove-duplicates xs)))) (define/match (derivative xs) [((list a b)) (list (- b a))] [((list* a b _)) (cons (- b a) (derivative (rest xs)))]) -(define (extrapolate xs) (if (constant? xs) (car xs) (+ (last xs) (extrapolate (derivative xs))))) +(define (extrapolate xs) + (if (constant? xs) + (car xs) + (+ (last xs) (extrapolate (derivative xs))))) ;; part 1 -(~>> histories (map extrapolate) (apply +)) +(~>> histories + (map extrapolate) + (apply +)) ;; part 2 -(~>> histories (map (λ~> reverse extrapolate)) (apply +)) +(~>> histories + (map (λ~> reverse extrapolate)) + (apply +)) diff --git a/aoc2023-other/day-10/day-10.rkt b/aoc2023-other/day-10/day-10.rkt index feaff64..64d8727 100644 --- a/aoc2023-other/day-10/day-10.rkt +++ b/aoc2023-other/day-10/day-10.rkt @@ -28,7 +28,8 @@ [(#\J) (list go-north go-west)]) (define (make-pipe-grid in) - (for*/hash ([(row r) (in-indexed (string-split in "\n"))] [(ch c) (in-indexed (string->list row))]) + (for*/hash ([(row r) (in-indexed (string-split in "\n"))] + [(ch c) (in-indexed (string->list row))]) (values (posn (add1 r) (add1 c)) ch))) (define (get-valid-S-neighbors S grid) @@ -74,7 +75,9 @@ (define (trace-ray pt pipes grid) (define row (posn-r pt)) - (for/fold ([acc 0] [corner #f] #:result acc) + (for/fold ([acc 0] + [corner #f] + #:result acc) ([col (in-naturals (posn-c pt))] #:do [(define test-pt (posn row col))] #:break (not (hash-has-key? grid test-pt)) @@ -89,4 +92,6 @@ [(#\L #\J) (values acc #f)] [(_ _) (values acc corner)]))) -(~> pipe-grid hash-keys (count (λ~> (trace-rays pipe-loop-set pipe-grid)) _)) +(~> pipe-grid + hash-keys + (count (λ~> (trace-rays pipe-loop-set pipe-grid)) _)) diff --git a/aoc2023-other/day-11/day-11.rkt b/aoc2023-other/day-11/day-11.rkt index 883ae11..dba617b 100644 --- a/aoc2023-other/day-11/day-11.rkt +++ b/aoc2023-other/day-11/day-11.rkt @@ -15,14 +15,18 @@ n)) (define (count-prior-empty-ranks rank empty-ranks) - (~> empty-ranks (takef (curryr < rank)) length)) + (~> 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 #\#)) + (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)]) diff --git a/aoc2023-other/day-12/day-12.rkt b/aoc2023-other/day-12/day-12.rkt index d07994f..50b14bb 100644 --- a/aoc2023-other/day-12/day-12.rkt +++ b/aoc2023-other/day-12/day-12.rkt @@ -12,43 +12,54 @@ [(list* template spring-set) (condition (string->list template) (map string->number spring-set))]))) -(define/memoize - (do-count template spring-group left-in-group need-gap?) - ;; template: list of spring positions - ;; spring-group: list of remaining contiguous groups of damaged springs - ;; left-in-group: springs remaining in current bad spring group being placed - ;; need-gap?: did we just finish placing a bad spring group - ;; and need at least one undamaged spring before starting the next one? - (match (list template spring-group left-in-group need-gap?) - ;; no springs left to place and no places left to place springs - ;; this is an OK arrangement, count it - [(list '() '() 0 _) 1] - ;; ambiguous wildcard, try both skipping this spot and starting a damaged spring group here - [(list (list* #\? t-rest) (list* g g-rest) 0 #f) - (+ (do-count t-rest g-rest (sub1 g) (= g 1)) (do-count t-rest spring-group 0 #f))] - ;; definitely a place for a good spring (.), move on without consuming any spring groups - [(or (list (list* #\? t-rest) '() 0 #f) ; good spring, no more damaged springs to place - (list (list* #\? t-rest) _ 0 #t) ; good spring right after finishing a group of bad springs - (list (list* #\. t-rest) _ 0 _)) ; known good spring - (do-count t-rest spring-group 0 #f)] - ;; start of bad spring (#) group, use the next spring group and remove 1 from it - [(list (list* #\# t-rest) (list* g g-rest) 0 #f) (do-count t-rest g-rest (sub1 g) (= g 1))] - ;; continuation of bad spring group, same - [(list (list* (or #\? #\#) t-rest) g left #f) (do-count t-rest g (sub1 left) (= left 1))] - ;; if nothing above works, this arrangement's invalid - [_ 0])) +(define/memoize (do-count template spring-group left-in-group need-gap?) + ;; template: list of spring positions + ;; spring-group: list of remaining contiguous groups of damaged springs + ;; left-in-group: springs remaining in current bad spring group being placed + ;; need-gap?: did we just finish placing a bad spring group + ;; and need at least one undamaged spring before starting the next one? + (match* (template spring-group left-in-group need-gap?) + ;; no springs left to place and no places left to place springs + ;; this is an OK arrangement, count it + [('() '() 0 _) 1] + ;; ambiguous wildcard, try both skipping this spot and starting a damaged spring group here + [((list* #\? t-rest) (list* g g-rest) 0 #f) + (+ (do-count t-rest g-rest (sub1 g) (= g 1)) + (do-count t-rest spring-group 0 #f))] + ;; definitely a place for a good spring (.), move on without consuming any spring groups + [((list* #\? t-rest) '() 0 #f) ; good spring, no more damaged springs to place + (do-count t-rest spring-group 0 #f)] + [((list* #\? t-rest) _ 0 #t) ; good spring right after finishing a group of bad springs + (do-count t-rest spring-group 0 #f)] + [((list* #\. t-rest) _ 0 _) ; known good spring + (do-count t-rest spring-group 0 #f)] + ;; start of bad spring (#) group, use the next spring group and remove 1 from it + [((list* #\# t-rest) (list* g g-rest) 0 #f) (do-count t-rest g-rest (sub1 g) (= g 1))] + ;; continuation of bad spring group, same + [((list* (or #\? #\#) t-rest) g left #f) (do-count t-rest g (sub1 left) (= left 1))] + ;; if nothing above works, this arrangement's invalid + [(_ _ _ _) 0])) (define (count-solutions c) (match-define (condition template spring-set) c) (do-count template spring-set 0 #f)) ;; part 1 -(for/sum ([c (in-list conditions)]) (count-solutions c)) +(for/sum ([c (in-list conditions)]) + (count-solutions c)) ;; part 2 (define expanded-conditions (for/list ([c (in-list conditions)]) - (condition (~> c condition-template (make-list 5 _) (add-between #\?) flatten) - (~> c condition-spring-set (make-list 5 _) flatten)))) + (condition (~> c + condition-template + (make-list 5 _) + (add-between #\?) + flatten) + (~> c + condition-spring-set + (make-list 5 _) + flatten)))) -(for/sum ([c* (in-list expanded-conditions)]) (count-solutions c*)) +(for/sum ([c* (in-list expanded-conditions)]) + (count-solutions c*)) diff --git a/aoc2023-other/day-14/day-14.rkt b/aoc2023-other/day-14/day-14.rkt index e90feba..d0b7cad 100644 --- a/aoc2023-other/day-14/day-14.rkt +++ b/aoc2023-other/day-14/day-14.rkt @@ -15,15 +15,21 @@ (~> col (chunks-by (curry equal? #\#)) (append-map (curryr sort char>?) _)))) (define (score board) - (for*/sum ([col (in-list board)] [(row n) (in-indexed (reverse col))] #:when (equal? row #\O)) + (for*/sum ([col (in-list board)] + [(row n) (in-indexed (reverse col))] + #:when (equal? row #\O)) (add1 n))) ;; part 1 -(~> input roll-boulders score) +(~> input + roll-boulders + score) ;; part 2 (define (rotate-board xss) - (~> xss (map reverse _) transpose)) + (~> xss + (map reverse _) + transpose)) (define (full-cycle board) (foldl (λ (_ acc) (~> acc roll-boulders rotate-board)) board (range 4))) diff --git a/aoc2023-other/day-16/day-16.rkt b/aoc2023-other/day-16/day-16.rkt index a21c8a2..4a70de8 100644 --- a/aoc2023-other/day-16/day-16.rkt +++ b/aoc2023-other/day-16/day-16.rkt @@ -67,4 +67,4 @@ (map (λ (c) (light (posn -1 c) 'down)) (range cols)) (map (λ (c) (light (posn rows c) 'up)) (range cols)))) -(time(get-energized (argmax get-energized all-starting-positions)))
\ No newline at end of file +(get-energized (argmax get-energized all-starting-positions))
\ No newline at end of file diff --git a/aoc2023-other/day-17/day-17.rkt b/aoc2023-other/day-17/day-17.rkt index 7d8c108..05709ad 100644 --- a/aoc2023-other/day-17/day-17.rkt +++ b/aoc2023-other/day-17/day-17.rkt @@ -4,8 +4,8 @@ threading data/heap) -(struct state (p heat-lost previous history) #:transparent) -(struct posn (r c) #:transparent) +(struct state (p heat-lost previous history)) +(struct posn (r c)) (define/match (add _p1 _p2) [((posn r1 c1) (posn r2 c2)) (posn (+ r1 r2) (+ c1 c2))]) @@ -25,11 +25,14 @@ (cons (state-p s) (same-dir s))) (define (goal? n s) - (and (equal? goal-posn (state-p s)) (>= (length (same-dir s)) n))) + (and (equal? goal-posn (state-p s)) + (>= (length (same-dir s)) n))) (define (same-dir s) (define history (state-history s)) - (if (empty? history) '() (takef history (λ (n) (equal? n (car history)))))) + (if (empty? history) + '() + (takef history (λ (n) (equal? n (car history)))))) (define (find-good-neighbors min-dist max-dist s) (match-define (state p hl prev hist) s) diff --git a/aoc2023-other/day-18/day-18.rkt b/aoc2023-other/day-18/day-18.rkt index c18a750..b589e41 100644 --- a/aoc2023-other/day-18/day-18.rkt +++ b/aoc2023-other/day-18/day-18.rkt @@ -2,7 +2,7 @@ (require advent-of-code threading) -(struct coord (x y) #:transparent) +(struct coord (x y)) (define input (~> (fetch-aoc-input (find-session) 2023 18 #:cache #true))) @@ -25,11 +25,14 @@ ([dig (in-list (string-split input "\n"))]) (define-values (dir dist) (parser dig)) (define next-coord (go-to-next-coord current-coord dir dist)) - (values (+ area (triangle-area current-coord next-coord)) (+ perimeter dist) next-coord))) + (values (+ area (triangle-area current-coord next-coord)) + (+ perimeter dist) next-coord))) ;; part 1 (define (parse-front dig) - (match-define (regexp #rx"(.) (.*) \\((.*)\\)" (list _ dir (app string->number dist) _hex)) dig) + (match-define (regexp #rx"(.) (.*) \\((.*)\\)" + (list _ dir (app string->number dist) _hex)) + dig) (values dir dist)) (find-area-using parse-front) @@ -37,7 +40,8 @@ ;; part 2 (define (parse-hex dig) - (match-define (regexp #rx".*\\(#(.....)(.)\\)" (list _ (app (curryr string->number 16) dist) dir)) + (match-define (regexp #rx".*\\(#(.....)(.)\\)" + (list _ (app (curryr string->number 16) dist) dir)) dig) (values dir dist)) diff --git a/aoc2023-other/day-19/day-19.rkt b/aoc2023-other/day-19/day-19.rkt index 9d9ad71..f7561f6 100644 --- a/aoc2023-other/day-19/day-19.rkt +++ b/aoc2023-other/day-19/day-19.rkt @@ -8,10 +8,10 @@ megaparsack/text racket/struct) -(struct part (x m a s) #:transparent) -(struct rule (rating comparison threshold action) #:transparent) -(struct just (action) #:transparent) -(struct interval (from to) #:transparent) +(struct part (x m a s)) +(struct rule (rating comparison threshold action)) +(struct just (action)) +(struct interval (from to)) (match-define (list raw-workflows raw-parts) (~> (fetch-aoc-input (find-session) 2023 19 #:cache #true) diff --git a/aoc2023-other/day-20/day-20.rkt b/aoc2023-other/day-20/day-20.rkt index 6d43192..2e3852d 100644 --- a/aoc2023-other/day-20/day-20.rkt +++ b/aoc2023-other/day-20/day-20.rkt @@ -8,9 +8,9 @@ megaparsack/text) (struct broadcaster ()) -(struct flipflop (state received) #:transparent) -(struct conjunction (recieved) #:transparent) -(struct cable (type dests) #:transparent) +(struct flipflop (state received)) +(struct conjunction (recieved)) +(struct cable (type dests)) (struct nothing ()) (define charlist->symbol (λ~>> (apply string) string->symbol)) @@ -24,7 +24,8 @@ (do (char/p #\&) [name <- (many+/p letter/p)] (pure (cons (charlist->symbol name) (conjunction (hash))))) - (do [name <- (many+/p letter/p)] (pure (cons (charlist->symbol name) (broadcaster))))))) + (do [name <- (many+/p letter/p)] + (pure (cons (charlist->symbol name) (broadcaster))))))) (define cable/p (do [mod <- module/p] @@ -38,22 +39,23 @@ (for/hash ([cable (in-list cables)]) (values (car (cable-type cable)) (cable-dests cable)))) +(define (set-conjunction-initial-state c) + (cond + [(conjunction? (cdr c)) + (~>> destinations + hash-keys + (filter (λ (k) (member (car c) (hash-ref destinations k)))) + (map (λ (k) (cons k 'low))) + (make-immutable-hash) + conjunction + (cons (car c)))] + [else c])) + (define (make-initial-conditions-hash cables) - (~> cables - (map cable-type _) - (map (λ (c) - (cond - [(conjunction? (cdr c)) - (~> destinations - hash-keys - (filter (λ (k) (member (car c) (hash-ref destinations k))) _) - (map (λ (k) (cons k 'low)) _) - (make-immutable-hash) - (conjunction) - (cons (car c) _))] - [else c])) - _) - make-immutable-hash)) + (~>> cables + (map cable-type) + (map set-conjunction-initial-state) + make-immutable-hash)) (define (receive mod from tone) (match mod diff --git a/aoc2023-other/day-21/day-21.rkt b/aoc2023-other/day-21/day-21.rkt index 8d73a7f..b5478eb 100644 --- a/aoc2023-other/day-21/day-21.rkt +++ b/aoc2023-other/day-21/day-21.rkt @@ -27,7 +27,9 @@ (for ([neighbor (in-list (neighbors cons))] #:when (hash-has-key? g neighbor)) (hash-set! update neighbor 'on))) (hash-union! g update #:combine/key (λ (_k _v v) v))) - (for/fold ([_ void] #:result (~>> g hash-values (count (curry equal? 'on)))) ([i (in-range n)]) + (for/fold ([_ void] + #:result (~>> g hash-values (count (curry equal? 'on)))) + ([i (in-range n)]) (displayln i) (make-one-step))) diff --git a/aoc2023-other/day-22/day-22.rkt b/aoc2023-other/day-22/day-22.rkt index 2abe4bf..53668c0 100644 --- a/aoc2023-other/day-22/day-22.rkt +++ b/aoc2023-other/day-22/day-22.rkt @@ -8,8 +8,8 @@ megaparsack/text racket/hash) -(struct posn (x y z) #:transparent) -(struct block (n from to) #:transparent) +(struct posn (x y z)) +(struct block (n from to)) (define input (fetch-aoc-input (find-session) 2023 22 #:cache #true)) diff --git a/aoc2023-other/day-23/day-23.rkt b/aoc2023-other/day-23/day-23.rkt index 3840187..c048013 100644 --- a/aoc2023-other/day-23/day-23.rkt +++ b/aoc2023-other/day-23/day-23.rkt @@ -10,7 +10,7 @@ [(col c) (in-indexed row)] #:when (not (equal? col #\#))) ; for now, we don't actually need to detect paths and slopes, just not-rocks - ; in part 1, all forks in the road go left or down, so we can just infer the path + ; in part 1, all forks in the road go right or down, so we can just infer the path ; direction from the shape of the junction and form a network of junctions from there ; in part 2, the slopes are removed anyway (values (cons (add1 r) (add1 c)) col))) @@ -23,7 +23,8 @@ (define (get-neighbors posn type) (match-define (cons r c) posn) (match type - ['junction (~> (set (cons (add1 r) c) (cons r (add1 c))))] + ['junction + (~> (set (cons (add1 r) c) (cons r (add1 c))))] [_ (~> (list (cons (add1 r) c) (cons (sub1 r) c) (cons r (add1 c)) (cons r (sub1 c))) (filter (curry hash-has-key? trails) _) @@ -41,7 +42,9 @@ [else (values k 'trail)]))) (define (walk-to-next-junction start current [length 1] [seen (set start)]) - (define next (~> current (get-neighbors _ 'trail) (set-subtract seen) set-first)) + (define next (~> current + (get-neighbors _ 'trail) + (set-subtract seen) set-first)) (cond [(equal? (hash-ref trails-with-junctions next) 'junction) (list (- (add1 length)) start next)] ; weird format is due to graph library @@ -77,7 +80,7 @@ [(equal? from to) acc] [else (define choices (filter (λ (path) (not (set-member? seen (car path)))) (hash-ref g from))) - (if (empty? choices) 0 ;; long dead-ends don't count + (if (empty? choices) 0 ; long dead-ends don't count (for/fold ([best acc]) ([path (in-list choices)] #:do [(match-define (cons next dist) path)]) diff --git a/aoc2023-other/day-24/day-24a.rkt b/aoc2023-other/day-24/day-24a.rkt index 39f559c..31f526d 100644 --- a/aoc2023-other/day-24/day-24a.rkt +++ b/aoc2023-other/day-24/day-24a.rkt @@ -12,10 +12,13 @@ (define LOWER-BOUND 200000000000000) (define UPPER-BOUND 400000000000000) +(define (->struct f str) + (~> str (string-split _ ",") (map (λ~> string-trim string->number) _) (apply f _))) + (define (parse-hail-record str) (match-define (list p v) (string-split str " @ ")) - (hail (~> p (string-split ",") (map (λ~> string-trim string->number) _) (apply posn _)) - (~> v (string-split ",") (map (λ~> string-trim string->number) _) (apply vel _)))) + (hail (->struct posn p) + (->struct vel v))) (define hail-paths (for/list ([hail (in-list (string-split input "\n"))]) @@ -28,16 +31,21 @@ (cond [(= (* vy1 vx2) (* vx1 vy2)) #f] [else - (define t1 (/ (- (* vy2 (- x1 x2)) (* vx2 (- y1 y2))) (- (* vy1 vx2) (* vx1 vy2)))) - (define t2 (/ (- (* vy1 (- x2 x1)) (* vx1 (- y2 y1))) (- (* vy2 vx1) (* vx2 vy1)))) + (define t1 (/ (- (* vy2 (- x1 x2)) (* vx2 (- y1 y2))) + (- (* vy1 vx2) (* vx1 vy2)))) + (define t2 (/ (- (* vy1 (- x2 x1)) (* vx1 (- y2 y1))) + (- (* vy2 vx1) (* vx2 vy1)))) (define x (+ x1 (* t1 vx1))) (define y (+ y1 (* t1 vy1))) - (and (<= LOWER-BOUND x UPPER-BOUND) (<= LOWER-BOUND y UPPER-BOUND) (<= 0 t1) (<= 0 t2))])) + (and (<= LOWER-BOUND x UPPER-BOUND) + (<= LOWER-BOUND y UPPER-BOUND) + (<= 0 t1) + (<= 0 t2))])) (for/sum ([(trial-paths) (in-combinations hail-paths 2)] ; #:when (apply valid-intersection? trial-paths)) - 1) + 1) ;; part 2 - see day-24b.rkt diff --git a/aoc2023-other/day-24/day-24b.rkt b/aoc2023-other/day-24/day-24b.rkt index f14cc94..b106b30 100644 --- a/aoc2023-other/day-24/day-24b.rkt +++ b/aoc2023-other/day-24/day-24b.rkt @@ -3,9 +3,9 @@ (require advent-of-code threading) -(struct hail (posn vel) #:transparent) -(struct posn (x y z) #:transparent) -(struct vel (x y z) #:transparent) +(struct hail (posn vel)) +(struct posn (x y z)) +(struct vel (x y z)) (define input (fetch-aoc-input (find-session) 2023 24 #:cache #true)) |