aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-other
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-08 02:59:26 -0500
committerHJ <thechairman@thechairman.info>2023-12-08 02:59:26 -0500
commitb9c4b4dfadd305c55fb09d421252780cb254f89b (patch)
treec92d27064bf177d72e7cc55a1eeee713e957ef63 /aoc2023-other
parentc82dee4b12a824ff73dc91f89445d4df75d3c876 (diff)
downloadgleam_aoc-b9c4b4dfadd305c55fb09d421252780cb254f89b.tar.gz
gleam_aoc-b9c4b4dfadd305c55fb09d421252780cb254f89b.zip
day 8 complete
Diffstat (limited to 'aoc2023-other')
-rw-r--r--aoc2023-other/day-06/day-06.rkt3
-rw-r--r--aoc2023-other/day-07/day-07.rkt49
2 files changed, 51 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..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)))