From b9c4b4dfadd305c55fb09d421252780cb254f89b Mon Sep 17 00:00:00 2001 From: HJ Date: Fri, 8 Dec 2023 02:59:26 -0500 Subject: day 8 complete --- aoc2023-other/day-06/day-06.rkt | 3 ++- aoc2023-other/day-07/day-07.rkt | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 aoc2023-other/day-07/day-07.rkt (limited to 'aoc2023-other') 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..67dc212 --- /dev/null +++ b/aoc2023-other/day-07/day-07.rkt @@ -0,0 +1,49 @@ +#lang racket + +(require advent-of-code + threading) + +(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) + (match-define (list card-str wager-str) (string-split str)) + (define cards (~> card-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 (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])) + +(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 h1 h2) + (define rank1 (identify-hand h1)) + (define rank2 (identify-hand h2)) + (if (= rank1 rank2) (compare-first-card (hand-cards h1) (hand-cards h2)) (< rank1 rank2))) + +;; part 1 + +(for/sum ([(h i) (in-indexed (~> input (map parse-hand _) (sort compare-hands)))]) + (* (add1 i) (hand-wager h))) -- cgit v1.2.3 From bf65cf56716dcaaafee0398acf44ef8f313eada0 Mon Sep 17 00:00:00 2001 From: HJ Date: Fri, 8 Dec 2023 08:00:32 -0500 Subject: day 8 racket complete --- aoc2023-other/day-08/day-08.rkt | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 aoc2023-other/day-08/day-08.rkt (limited to 'aoc2023-other') 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)) -- cgit v1.2.3 From a722e28a5f4e7a9c11ecc6c58e733a33b072052d Mon Sep 17 00:00:00 2001 From: HJ Date: Fri, 8 Dec 2023 08:17:56 -0500 Subject: day 7 racket complete --- aoc2023-other/day-07/day-07.rkt | 52 ++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'aoc2023-other') diff --git a/aoc2023-other/day-07/day-07.rkt b/aoc2023-other/day-07/day-07.rkt index 67dc212..d2311c8 100644 --- a/aoc2023-other/day-07/day-07.rkt +++ b/aoc2023-other/day-07/day-07.rkt @@ -1,7 +1,8 @@ #lang racket (require advent-of-code - threading) + threading + memo) (struct hand (cards wager) #:transparent) @@ -22,28 +23,47 @@ (define input (~> (open-aoc-input (find-session) 2023 7 #:cache #true) port->lines)) -(define (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])) +(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 h1 h2) - (define rank1 (identify-hand h1)) - (define rank2 (identify-hand h2)) +(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 -(for/sum ([(h i) (in-indexed (~> input (map parse-hand _) (sort compare-hands)))]) - (* (add1 i) (hand-wager h))) +(define (compare-hands-no-wilds h1 h2) + (compare-hands identify-hand h1 h2)) + +(define (total-score with in) + (for/sum ([(h i) (in-indexed (~> in (map parse-hand _) (sort with)))]) (* (add1 i) (hand-wager h)))) + +(total-score compare-hands-no-wilds 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 compare-hands-with-wilds (map (curryr string-replace "J" "*") input)) -- cgit v1.2.3 From b51dd4b3768bce0209733ef2562cb96e5330d3c6 Mon Sep 17 00:00:00 2001 From: HJ Date: Fri, 8 Dec 2023 08:26:37 -0500 Subject: day 7 racket complete --- aoc2023-other/day-07/day-07.rkt | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'aoc2023-other') diff --git a/aoc2023-other/day-07/day-07.rkt b/aoc2023-other/day-07/day-07.rkt index d2311c8..f0a40ea 100644 --- a/aoc2023-other/day-07/day-07.rkt +++ b/aoc2023-other/day-07/day-07.rkt @@ -15,9 +15,13 @@ [(#\T) 10] [(#\*) 1]) -(define (parse-hand str) +(define (parse-hand str #:jokers [jokers? #f]) (match-define (list card-str wager-str) (string-split str)) - (define cards (~> card-str string->list (map card->int _))) + (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)) @@ -50,10 +54,14 @@ (define (compare-hands-no-wilds h1 h2) (compare-hands identify-hand h1 h2)) -(define (total-score with in) - (for/sum ([(h i) (in-indexed (~> in (map parse-hand _) (sort with)))]) (* (add1 i) (hand-wager h)))) +(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 compare-hands-no-wilds input) +(total-score input) ;; part 2 @@ -66,4 +74,4 @@ (define (compare-hands-with-wilds h1 h2) (compare-hands (λ~> find-best-joker-substitution identify-hand) h1 h2)) -(total-score compare-hands-with-wilds (map (curryr string-replace "J" "*") input)) +(total-score input #:jokers #true) -- cgit v1.2.3