Skip to content

Commit

Permalink
Finished documenting
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Dec 7, 2023
1 parent 1efc023 commit e4d6602
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2453,19 +2453,21 @@
"Rules for determining hand strengh:\n",
"\n",
"- Usual poker hand strengths, i.e.\n",
" - `5-of-a-kind > 4-of-a-kind`\n",
" - ` > full-house`\n",
" - ` > 3-of-a-kind`\n",
" - ` > two pair`\n",
" - ` > one pair`\n",
" - ` > high card`\n",
" ```\n",
" 5-of-a-kind > 4-of-a-kind\n",
" > full-house\n",
" > 3-of-a-kind\n",
" > two pair\n",
" > one pair\n",
" > high card\n",
" ```\n",
"- But unlike normal poker, hand strength tie-breakers are achieved by comparing the successive card strengths of cards in the hand, starting from first to last.\n",
"\n",
"**My solution:**\n",
"\n",
"- Create a dict to store `CARD_SCORES`. I've assigned integer values so we can compare card strength.\n",
"- Create a dict to store `HAND_TYPE`. Again, I've assigned integer values so that we can compare hands.\n",
"- Create a `Hand` class:\n",
" - In this class, create a dict class attribute to store `card_scores`. I've assigned integer values so we can compare card strength.\n",
" - Create a dict class attribute to store `HAND_TYPE`. Again, I've assigned integer values so that we can compare hands.\n",
" - Initialise with a string that represents our five cards.\n",
" - I'm using the Python `collections.Counter` class to help me determine the `HAND_TYPE`. The `Counter` class counts members of any collection passed to it, including strings. It stores the counts as a dict, in the form `{character: count}`. Then we can use `most_common()` to convert this dict into an ordered list of tuples, ordered by count.\n",
" - From here, it's trivial to determine the hand type. For example, if `most_common()` count is 5, then we have five-of-a-kind. If it's 4, then we have four-of-a-kind, and so on. Once we've determined the hand type, store it in the `self._hand_type` instance variable.\n",
Expand Down Expand Up @@ -2510,34 +2512,32 @@
"metadata": {},
"outputs": [],
"source": [
"CARD_SCORES = { # card labels\n",
" \"A\": 14, \n",
" \"K\": 13,\n",
" \"Q\": 12,\n",
" \"J\": 11,\n",
" \"T\": 10,\n",
" \"9\": 9,\n",
" \"8\": 8,\n",
" \"7\": 7,\n",
" \"6\": 6,\n",
" \"5\": 5,\n",
" \"4\": 4,\n",
" \"3\": 3,\n",
" \"2\": 2\n",
"}\n",
"\n",
"HAND_TYPE = {\n",
" \"FIVE\": 7,\n",
" \"FOUR\": 6,\n",
" \"FH\": 5,\n",
" \"THREE\": 4,\n",
" \"TP\": 3,\n",
" \"OP\": 2,\n",
" \"HC\": 1\n",
"}\n",
"\n",
"class Hand:\n",
" card_scores = CARD_SCORES\n",
" card_scores = { # card labels\n",
" \"A\": 14, \n",
" \"K\": 13,\n",
" \"Q\": 12,\n",
" \"J\": 11,\n",
" \"T\": 10,\n",
" \"9\": 9,\n",
" \"8\": 8,\n",
" \"7\": 7,\n",
" \"6\": 6,\n",
" \"5\": 5,\n",
" \"4\": 4,\n",
" \"3\": 3,\n",
" \"2\": 2\n",
" }\n",
" \n",
" HAND_TYPE = {\n",
" \"FIVE\": 7,\n",
" \"FOUR\": 6,\n",
" \"FH\": 5,\n",
" \"THREE\": 4,\n",
" \"TP\": 3,\n",
" \"OP\": 2,\n",
" \"HC\": 1\n",
" }\n",
" \n",
" def __init__(self, cards: str, joker=False) -> None:\n",
" self.cards = cards\n",
Expand Down Expand Up @@ -2591,7 +2591,7 @@
" \n",
" def value(self):\n",
" \"\"\" Return a score, based on hand type \"\"\"\n",
" return HAND_TYPE[self._hand_type]\n",
" return Hand.HAND_TYPE[self._hand_type]\n",
" \n",
" def __lt__(self, other: Hand):\n",
" \"\"\" Compare this hand with another hand.\n",
Expand Down

0 comments on commit e4d6602

Please sign in to comment.