Day 7 Part 1 - Corrected

This commit is contained in:
Daniel Ziltener 2023-12-08 01:12:02 +01:00
parent f52b5fdcdf
commit c83a433bb8
Signed by: zilti
GPG Key ID: B38976E82C9DAE42
1 changed files with 52 additions and 13 deletions

View File

@ -2988,6 +2988,7 @@ wins; otherwise, continue with the third card in each hand, then the fourth, the
(define (hand-cards> cards-a cards-b)
(or (card> (car cards-a) (car cards-b))
(and (< 1 (length cards-a))
(= (card-weight (car cards-a)) (card-weight (car cards-b)))
(hand-cards> (cdr cards-a) (cdr cards-b)))))
(define (hands> hand-a hand-b)
@ -3020,7 +3021,6 @@ because its third card is stronger (and both hands have the same first and secon
To play Camel Cards, you are given a list of hands and their corresponding *bid* (your puzzle
input). For example:
#+NAME: day7-part1-hand-input-example
#+begin_example
32T3K 765
T55J5 684
@ -3029,6 +3029,30 @@ KTJJT 220
QQQJA 483
#+end_example
Better sample input from [[https://old.reddit.com/r/adventofcode/comments/18cr4xr/2023_day_7_better_example_input_not_a_spoiler/][u/LxsterGames]] on Reddit:
#+NAME: day7-part1-hand-input-example
#+begin_example
2345A 1
Q2KJJ 13
Q2Q2Q 19
T3T3J 17
T3Q33 11
2345J 3
J345A 2
32T3K 5
T55J5 29
KK677 7
KTJJT 34
QQQJA 31
JJJJJ 37
JAAAA 43
AAAAJ 59
AAAAA 61
2AAAA 23
2JJJJ 53
JJJJ2 41
#+end_example
#+NAME: day7-part1-data-extraction
#+begin_src scheme :tangle day7.scm
(define camel-card-irregex
@ -3081,26 +3105,41 @@ So, the first step is to put the hands in order of strength:
(let ((hands (input->hands input)))
(map calc-hand-type hands)
(rank-hands (reverse (sort hands hands>)))
(append '(("Cards" "Rank" "Bid" "Bid*Rank"))
(append '(("Cards" "Hand Type" "Rank" "Bid" "Bid*Rank"))
(map (lambda (hand)
(let ((card-labels (map card-label (hand-cards hand)))
(hand-rank (hand-rank hand))
(hand-bid (hand-bid hand)))
(hand-bid (hand-bid hand))
(hand-type (hand-type-label (hand-type-rec hand))))
(list (apply string-append (map keyword->string card-labels))
hand-rank hand-bid
hand-type hand-rank hand-bid
(* hand-bid hand-rank))))
(sort hands hands>))
`(("Total" "" "" ,(total-winnings hands)))))
`(("Total" "" "" "" ,(total-winnings hands)))))
#+end_src
#+RESULTS:
| Cards | Rank | Bid | Bid*Rank |
| QQQJA | 5 | 483 | 2415 |
| T55J5 | 4 | 684 | 2736 |
| KK677 | 3 | 28 | 84 |
| KTJJT | 2 | 220 | 440 |
| 32T3K | 1 | 765 | 765 |
| Total | | | 6440 |
| Cards | Hand Type | Rank | Bid | Bid*Rank |
| AAAAA | five-of-a-kind | 19 | 61 | 1159 |
| JJJJJ | five-of-a-kind | 18 | 37 | 666 |
| AAAAJ | four-of-a-kind | 17 | 59 | 1003 |
| JAAAA | four-of-a-kind | 16 | 43 | 688 |
| JJJJ2 | four-of-a-kind | 15 | 41 | 615 |
| 2AAAA | four-of-a-kind | 14 | 23 | 322 |
| 2JJJJ | four-of-a-kind | 13 | 53 | 689 |
| Q2Q2Q | full-house | 12 | 19 | 228 |
| QQQJA | three-of-a-kind | 11 | 31 | 341 |
| T55J5 | three-of-a-kind | 10 | 29 | 290 |
| T3Q33 | three-of-a-kind | 9 | 11 | 99 |
| KK677 | two-pair | 8 | 7 | 56 |
| KTJJT | two-pair | 7 | 34 | 238 |
| T3T3J | two-pair | 6 | 17 | 102 |
| Q2KJJ | one-pair | 5 | 13 | 65 |
| 32T3K | one-pair | 4 | 5 | 20 |
| J345A | high-card | 3 | 2 | 6 |
| 2345A | high-card | 2 | 1 | 2 |
| 2345J | high-card | 1 | 3 | 3 |
| Total | | | | 6592 |
Now, you can determine the total winnings of this set of hands by adding up the result of
multiplying each hand's bid with its rank (=765= * 1 + =220= * 2 + =28= * 3 + =684= * 4 + =483= *
@ -3139,7 +3178,7 @@ Find the rank of every hand in your set. *What are the total winnings?*
#+CALL: day7-part1-calc-fn[:epilogue "(calc-part-1)"]()
#+RESULTS:
: 250554136
: 251216224
** Part Two