r/adventofcode • u/Afkadrian • Dec 07 '23
Spoilers [2023 Day 7] An interesting algorithm
I found out that if we find the count of the card that appears the most times and subtract the amount of different cards, the result is unique for each type of hand.
In other words... max_count - different_cards is a discriminant.
For a Rust implementation, check the from_cards function in my repo.
Has anyone else found this pattern?
46
Upvotes
4
u/Symbroson Dec 07 '23
You can also sum the squares of unique card amounts based on shannons entropy as suggested by u/sinsworth here. This can be directly used as sorting property
my implementation
2
u/Hoinkas Dec 07 '23 edited Dec 07 '23
I don't understand. I tried to implement it as follows:
for hand, bid in listOfHands:return max(Counter(hand).values()) - len(set(hand))and for example T55J5 and QQQJA have the same result (which is 0).
T55J5 => max_count = 3 (for 5), different_cards = 3 (T5J) => 3-3=0
QQQJA => max_count = 3 (for Q), different_cards = 3 (QJA) => 3-3=0
What did I miss?
11
u/Practical_Hat8489 Dec 07 '23
That's a cool observation. My language compares arrays lexicographically (`[5] > [4, 1] > [3, 2] > [3, 1, 1] > [2, 2, 1] > [2, 1, 1, 1] > [1, 1, 1, 1, 1]`) so I just went comparing the sorted descendingly arrays of numbers of occurrences, but that's a nice substitute for the languages that can't.
4
u/paynedigital Dec 07 '23
Yep. I got there by originally calculating both the max count and distinct cards anyway and exhaustively enumerating each case (“if five of a kind”, “if four of a kind”) using the distinct count as a tie breaker when needed, then slowly collapsing the various tests where I could (“if four of a kind or greater”), before realising the few I had left could be collapsed completely.
Slow, but satisfying!
1
u/Queueue_ Dec 07 '23
Just tested this by ripping out my custom solution for categorizing the initial strength of a hand and replaced it with
and everything still worked. Cool find!
1
u/spliznork Dec 07 '23
Oh that's neat. And then jokers 1) don't count towards different_cards and 2) just increment max_count, done. Amazing!
2
u/AverageBeef Dec 07 '23
I think I got half way there. I first checked the length of each hand as a hash set, then in cases with the same length, I sorted them out via greatest count.
2
u/[deleted] Dec 08 '23
My pattern was to find the total number of each type of card, then take the sum of the squares. But sum of squares isn't necessarily the only version of this. Any superlinear function could replace squaring.
2
u/Dalv2 Dec 08 '23
I found something similar. I found that if you take the factorial of how many times a card appears then thats also a unique identifier, and in code this is simple to do, as you're looping over the cards you just multiply the result variable by how many times that card has showed up (using an array where a[x] = how many times x shows up in the hand).
Five of a kind: 5! = 120 Four of a kind: 4! * 1! = 24 Full house: 3! * 2! = 12 Three of a kind: 3! * 1! * 1! = 6 Two pair: 2! * 2! * 1! = 4 One pair : 2! * 1! * 1! * 1! = 2 High card is just 1! repeated 5 times so it's 1
1
u/[deleted] Dec 07 '23
clever, I didnt notice that :)
1
u/0x4A6F654D616D61 Dec 07 '23 edited Dec 07 '23
I did simillar thing, i counted how many different numbers there are and how much a number is repeated (highest value from all of the diffrent numbers). Then i've made some if statements ([repeats, amount of numbers]):
Then for part 2 it was a matter of not counting J's to these 2 values but rather counting all J's as joker count and then adding joker count to highest repeat count of any number other than J and all of previous statements work perfectly fine.
The thing that made today's puzzle a living hell for me was sorting all cards of different types (high card, one pair etc.) In alphabetical-like order (I was coding in C, so no built-in functon to do that), so I spent 5 hours trying to come up for that until I googled algorithm for alphabetical sort os string arrays and slightly modified it
My code in C: https://github.com/dis-Is-Fine/advent-of-code/blob/master/day%207/
1
u/Ayl_rs Dec 07 '23
Yea I did something similar in Go.
I calculated the count of each card and the max
1
u/KrastyBasty Dec 08 '23
i knew there was a way but I just couldn't get to it. thanks for posting
1
u/MaxMahem Dec 08 '23
I came up with this sort of pseudo-bitmask approach. If given a list of the count of times a card has been seen:
This results in a unique value for each hand:
Now, just trying to adapt this approach for jokers.
1
u/stribor14 Dec 08 '23
Great catch, kudos, that shortened my code by 20 lines as it can also be adopted for part 2. Previously I had if-else depending on the number of jokers, pairs, tripples, etc.
1
u/MacHeath80 Dec 08 '23
I came up with 5 - amount of different cards + highest amount of cards. This maps all hand types to a number between 9 (five of a kind) and 1 (high card) and in the correct order.
4
u/[deleted] Dec 07 '23
[removed] — view removed comment
2
u/[deleted] Dec 07 '23
[removed] — view removed comment