annotate day7.txt @ 11:a7fb64b48830

Implement Day 8 - node/map walking Cheated on part 2 by finding out that the routes loop and you need to do the lowest-common-multiple to get a VERY large number
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Dec 2023 16:37:20 +0000
parents 9ec95ff0d33d
children 1e16a25a9553
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 --- Day 7: Camel Cards ---
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 Your all-expenses-paid trip turns out to be a one-way, five-minute ride in an airship. (At least it's a cool airship!) It drops you off at the edge of a vast desert and descends back to Island Island.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 "Did you bring the parts?"
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 You turn around to see an Elf completely covered in white clothing, wearing goggles, and riding a large camel.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 "Did you bring the parts?" she asks again, louder this time. You aren't sure what parts she's looking for; you're here to figure out why the sand stopped.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 "The parts! For the sand, yes! Come with me; I will show you." She beckons you onto the camel.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 After riding a bit across the sands of Desert Island, you can see what look like very large rocks covering half of the horizon. The Elf explains that the rocks are all along the part of Desert Island that is directly above Island Island, making it hard to even get there. Normally, they use big machines to move the rocks and filter the sand, but the machines have broken down because Desert Island recently stopped receiving the parts they need to fix the machines.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 You've already assumed it'll be your job to figure out why the parts stopped when she asks if you can help. You agree automatically.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 Because the journey will take a few days, she offers to teach you the game of Camel Cards. Camel Cards is sort of similar to poker except it's designed to be easier to play while riding a camel.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 In Camel Cards, you get a list of hands, and your goal is to order them based on the strength of each hand. A hand consists of five cards labeled one of A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, or 2. The relative strength of each card follows this order, where A is the highest and 2 is the lowest.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 Every hand is exactly one type. From strongest to weakest, they are:
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 Five of a kind, where all five cards have the same label: AAAAA
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 Four of a kind, where four cards have the same label and one card has a different label: AA8AA
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 Full house, where three cards have the same label, and the remaining two cards share a different label: 23332
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 Three of a kind, where three cards have the same label, and the remaining two cards are each different from any other card in the hand: TTT98
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 Two pair, where two cards share one label, two other cards share a second label, and the remaining card has a third label: 23432
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 One pair, where two cards share one label, and the other three cards have a different label from the pair and each other: A23A4
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 High card, where all cards' labels are distinct: 23456
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 Hands are primarily ordered based on type; for example, every full house is stronger than any three of a kind.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 If two hands have the same type, a second ordering rule takes effect. Start by comparing the first card in each hand. If these cards are different, the hand with the stronger first card is considered stronger. If the first card in each hand have the same label, however, then move on to considering the second card in each hand. If they differ, the hand with the higher second card wins; otherwise, continue with the third card in each hand, then the fourth, then the fifth.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 So, 33332 and 2AAAA are both four of a kind hands, but 33332 is stronger because its first card is stronger. Similarly, 77888 and 77788 are both a full house, but 77888 is stronger because its third card is stronger (and both hands have the same first and second card).
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 To play Camel Cards, you are given a list of hands and their corresponding bid (your puzzle input). For example:
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 32T3K 765
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 T55J5 684
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 KK677 28
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 KTJJT 220
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 QQQJA 483
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 This example shows five hands; each hand is followed by its bid amount. Each hand wins an amount equal to its bid multiplied by its rank, where the weakest hand gets rank 1, the second-weakest hand gets rank 2, and so on up to the strongest hand. Because there are five hands in this example, the strongest hand will have rank 5 and its bid will be multiplied by 5.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 So, the first step is to put the hands in order of strength:
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 32T3K is the only one pair and the other hands are all a stronger type, so it gets rank 1.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 KK677 and KTJJT are both two pair. Their first cards both have the same label, but the second card of KK677 is stronger (K vs T), so KTJJT gets rank 2 and KK677 gets rank 3.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 T55J5 and QQQJA are both three of a kind. QQQJA has a stronger first card, so it gets rank 5 and T55J5 gets rank 4.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 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 * 5). So the total winnings in this example are 6440.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 Find the rank of every hand in your set. What are the total winnings?
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 --- Part Two ---
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 To make things a little more interesting, the Elf introduces one additional rule. Now, J cards are jokers - wildcards that can act like whatever card would make the hand the strongest type possible.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 To balance this, J cards are now the weakest individual cards, weaker even than 2. The other cards stay in the same order: A, K, Q, T, 9, 8, 7, 6, 5, 4, 3, 2, J.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 J cards can pretend to be whatever card is best for the purpose of determining hand type; for example, QJJQ2 is now considered four of a kind. However, for the purpose of breaking ties between two hands of the same type, J is always treated as J, not the card it's pretending to be: JKKK2 is weaker than QQQQ2 because J is weaker than Q.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 Now, the above example goes very differently:
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 32T3K 765
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 T55J5 684
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 KK677 28
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 KTJJT 220
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 QQQJA 483
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 32T3K is still the only one pair; it doesn't contain any jokers, so its strength doesn't increase.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 KK677 is now the only two pair, making it the second-weakest hand.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 T55J5, KTJJT, and QQQJA are now all four of a kind! T55J5 gets rank 3, QQQJA gets rank 4, and KTJJT gets rank 5.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 With the new joker rule, the total winnings in this example are 5905.
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79
9ec95ff0d33d Add Day 7 solutions with string sorting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 Using the new joker rule, find the rank of every hand in your set. What are the new total winnings?