Ben Yafai

Advent Of Code

7th December 2023

2023 - Day 7

Camel Cards

Today's challenge is a simplified poker-like game where 5 cards get ranked by how many cards in each hand are the same. If two hands are ranked the same, then the hand with the highest card wins.

As ever, I regexed out my data from the input;


I chucked together a quick array to get our card values


My first thought was that a usort was the way to go, here's the inline function


This was quite fun and relatively simple. The wide range of php array functions helped a lot.

Part two of course always makes it more complex. Now the Jack becomes a Joker wildcard that can be substitued for any other card, however, to balance it out, only has a score of 1 instead of the previous 11.

I updated the cards array for the new score


I added a little bit of code into the middle of the sort in order to identify the cards and make our substitutions


The first problem I had was unsetting a full hand of Jokers JJJJJ with nothing and producing errors - a quick find.

Then I got the wrong answer because I saw hands like JJJJQ which my code would see that the top result was 4xJ and would try to replace the result exactly as it was, hence the if ($keys[0]!= 1) to make sure I wasn't updating the wrong card.

My full responses are found on GitHub