This is Day 7 of Advent of Code 2023! If you would like to solve the problems before looking at the solution, you can find them here.
In part 1 of Day 7, we're playing poker, but not really. I feel like my knowledge of poker was actually a problem in this case as I kept introducing poker rules into my code. For example, the puzzle and I have very different ideas of tie-breaks in poker, but then again, this is CAMEL CARDS, meant to be easier to play while riding a camel. The primary goal here is to rank the hands based on a poker-like system, with slightly different tie break rules - the tie break is calculated sequentially. The ordering is then used to rank-multiply the bets and the sum is the final answer for part 1. Click here to jump to the part 2 discussion.
For part 2 of Day 7, there is a twist! The J that used to be a Jack is now a Joker, meaning that it can be used as any card that would make the hand the strongest. However, the Joker by itself is the lowest card, even lower than a 2, a fact that is important in ties - it took me a while to debug this! For example, a AAJAJ hand would be a Five of a kind with both jokers as Aces, as would a AJAAJ. However, the first hand beats the second hand because of the comparison of the second card. I had to come up with a system that stored not only the 'jokerified' hand for the hand ranking but also the original hand for tie breaks - I did so by concatenating the two hands as new-old. For example, the hand AAJAJ that we mentioned earlier would be stored as AAAAA-AAJAJ. Cards that had no jokers were stored as themselves. Using this system, we can determine the new order and obtain the rank-multiplication of the bets again, the sum of which is the final answer for part 2.
That's all folks! If you made it this far, enjoy some AI art generated by u/dimpopo using the prompt for this puzzle.
Cheers, Devang