r/adventofcode • • 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?

44 Upvotes

38 comments sorted by

View all comments

Show parent comment

3

u/somebodddy Dec 07 '23

Yea, it really is faster than what I was using before (matching the array pattern to pick the hand type from an enum) - brought me from ~250 microseconds to ~235. But more importantly - it decreased the amount of code:

$ git diff --compact-summary
 src/day7.rs | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)