diff options
Diffstat (limited to 'aoc2023-other')
-rw-r--r-- | aoc2023-other/day-06/day-06.rkt | 3 | ||||
-rw-r--r-- | aoc2023-other/day-07/day-07.rkt | 77 | ||||
-rw-r--r-- | aoc2023-other/day-08/day-08.rkt | 32 |
3 files changed, 111 insertions, 1 deletions
diff --git a/aoc2023-other/day-06/day-06.rkt b/aoc2023-other/day-06/day-06.rkt index 2897821..399c131 100644 --- a/aoc2023-other/day-06/day-06.rkt +++ b/aoc2023-other/day-06/day-06.rkt @@ -1,7 +1,8 @@ #lang racket (require advent-of-code - threading) + threading + ) (match-define (list times distances) (~> (open-aoc-input (find-session) 2023 6 #:cache #true) port->lines)) diff --git a/aoc2023-other/day-07/day-07.rkt b/aoc2023-other/day-07/day-07.rkt new file mode 100644 index 0000000..f0a40ea --- /dev/null +++ b/aoc2023-other/day-07/day-07.rkt @@ -0,0 +1,77 @@ +#lang racket + +(require advent-of-code + threading + memo) + +(struct hand (cards wager) #:transparent) + +(define/match (card->int card) + [((? char-numeric?)) (~> card string string->number)] + [(#\A) 14] + [(#\K) 13] + [(#\Q) 12] + [(#\J) 11] + [(#\T) 10] + [(#\*) 1]) + +(define (parse-hand str #:jokers [jokers? #f]) + (match-define (list card-str wager-str) (string-split str)) + (define cards + (~> card-str + ((λ (str) (if jokers? (string-replace str "J" "*") str))) + string->list + (map card->int _))) + (define wager (~> wager-str string->number)) + (hand cards wager)) + +(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 (compare-first-card cs1 cs2) + (if (= (first cs1) (first cs2)) + (compare-first-card (rest cs1) (rest cs2)) + (< (first cs1) (first cs2)))) + +(define (compare-hands with h1 h2) + (define rank1 (with h1)) + (define rank2 (with h2)) + (if (= rank1 rank2) (compare-first-card (hand-cards h1) (hand-cards h2)) (< rank1 rank2))) + +;; part 1 + +(define (compare-hands-no-wilds h1 h2) + (compare-hands identify-hand h1 h2)) + +(define (total-score in #:jokers [jokers? #false]) + (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)))) + +(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))) + +(define (compare-hands-with-wilds h1 h2) + (compare-hands (λ~> find-best-joker-substitution identify-hand) h1 h2)) + +(total-score input #:jokers #true) diff --git a/aoc2023-other/day-08/day-08.rkt b/aoc2023-other/day-08/day-08.rkt new file mode 100644 index 0000000..bc234b5 --- /dev/null +++ b/aoc2023-other/day-08/day-08.rkt @@ -0,0 +1,32 @@ +#lang racket + +(require advent-of-code + threading) + +(struct exits (left right) #:transparent) + +(match-define (list raw-directions raw-maze) + (~> (fetch-aoc-input (find-session) 2023 8 #:cache #true) (string-split "\n\n"))) + +(define directions (string->list raw-directions)) + +(define maze + (for/hash ([line (in-list (string-split raw-maze "\n"))]) + (match (regexp-match #rx"(...) = \\((...), (...)\\)" line) + [(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)]) + #:break (string-suffix? current end) + (define node (hash-ref maze current)) + (case dir + [(#\L) (values (exits-left node) (add1 acc))] + [(#\R) (values (exits-right node) (add1 acc))]))) + +;; part 1 +(to-next-node "AAA" "ZZZ" directions maze) + +;; 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)) |