aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-08 08:26:37 -0500
committerHJ <thechairman@thechairman.info>2023-12-08 08:26:37 -0500
commitb51dd4b3768bce0209733ef2562cb96e5330d3c6 (patch)
treef941d29b69baf05388e49814ea27aa7f33e26354
parenta722e28a5f4e7a9c11ecc6c58e733a33b072052d (diff)
downloadgleam_aoc-b51dd4b3768bce0209733ef2562cb96e5330d3c6.tar.gz
gleam_aoc-b51dd4b3768bce0209733ef2562cb96e5330d3c6.zip
day 7 racket complete
-rw-r--r--aoc2023-other/day-07/day-07.rkt20
1 files changed, 14 insertions, 6 deletions
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)