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