Brute Force, Binomials and Balatro

Paul Stanford · Permutive · London Scala User Group

What is Balatro?

A typical Balatro round

seed 94IKWPES, if you want to play it yourself

Jokers

Joker

Joker: +4 Mult

+4 Mult

Greedy Joker

Greedy Joker: Played cards with Diamond suit give +3 Mult when scored

Played cards with Diamond suit give +3 Mult when scored

Crazy Joker

Crazy Joker: +12 Mult if played hand contains a Straight

+12 Mult if played hand contains a Straight

Runner

Runner: Gains +15 Chips if played hand contains a Straight

Gains +15 Chips if played hand contains a Straight

All of these jokers just help increase the score

Shortcut Joker

The Shortcut Joker
Allows Straights to be made with gaps of 1 rank (ex: 10 8 6 5 3)
3 5 6 8 10 laid out by rank, with empty slots where the 4, 7 and 9 would be

This Joker fundamentally changes the rules!

The Question

Playing with the Shortcut Joker I felt like I was getting loads of straights easily! But can this be quantified?

Given a standard 52 card deck what is the probability that the first 8 cards I draw contain a straight where gaps of one are allowed?

But first, a simpler question

Given a standard 52 card deck what is the probability that the first 8 cards I draw contain a normal straight?

What is a Straight?

5 6 7 8 9 in mixed suits
✓
A 2 3 4 5 in mixed suits
✓
10 J Q K A in mixed suits
✓
Q K A 2 3: not a straight
✗
An 8-card hand 3 5 6 7 8 9 J K, which contains the straight 5 to 9
✓

Brute Force

You just do it, and it works: 73,870,336 of 752,538,150 hands, 9.82%

  1. But it tells me nothing about the problem. I still don't know why it's 9.82%
  2. And it took 3 min 38 s

Brute Force, Binomials and Balatro

Paul Stanford · Permutive · London Scala User Group

A portrait of an eighteenth-century mathematician

Mathematical
Solution

  1. Simplest case: a hand of 5 cards
  2. More simple cases, to build intuition
  3. The general case
  4. The general case, in Scala
  5. Introduce gaps

5 cards

First we can work out how many straights exist if we ignore suits

To do this we need only consider the rank sets of a hand

I define a "rank set" as the set of distinct ranks in a hand

The ten straights, A-2-3-4-5 up to 10-J-Q-K-A, as rank sets: ranks only, no suits

5 cards

For each card we have 4 choices of suit, and there are 5 cards

\(4^5 = 1{,}024\)

This is just a handful of examples with the first rank set, A to 5. Well, a few handfuls I guess!

Ten ways to suit A-2-3-4-5, from all spades to all four suits

5 cards

All 1,024 ways to choose suits for one rank set

♠ saffron
♥ hot coral
♣ cerulean
♦ deep purple
All 1,024 suit assignments of A-2-3-4-5 as a grid of coloured cells

5 cards

\(S = 10\) the number of rank sets that give a straight
\(Su = 4^5 = 1{,}024\) the suit factor: the number of ways to choose suits for a given rank set
\(S \times Su = 10{,}240\)

Six-Card Hands

How many hands of 6 cards contain a straight?

A 3-to-7 straight of diamonds plus an empty slot for the sixth card

10,240 5-card straights multiplied by 47 choices of sixth card = 481,280

But brute force says 367,616. So what did I do wrong?

Double Counting Demons

The same six-card hand built two ways: 3-7 of diamonds plus the 2, or 2 to 6 plus the 7

If I have a run of 6 cards there's always going to be 2 ways to get to it!

Devious Double Counting Demons

The same six-card hand built two ways: 3-7 of diamonds plus the 3 of clubs, or 3 of clubs to 7 plus the 3 of diamonds

More subtly if I have a hand with a straight and a duplicate rank there's also going to be two ways to get to it!

Two Classes of Demon

There are two different types of double counting which correspond to different "templates" for the hand; the difference is the number of distinct ranks in each hand

Each template can be thought of as a multiset

Three six-card hands with six distinct ranks: 3 to 7 of diamonds plus a 9, 3 to 8, and 9 to King plus a 4

six distinct ranks

\(\{1, 1, 1, 1, 1, 1\}\)

Three six-card hands with one pair: 3 to 7 of diamonds plus a second 3, plus a second 6, and 9 to King plus a second Queen

five distinct ranks, one pair

\(\{2, 1, 1, 1, 1\}\)

Multisets

A set is an unordered collection of objects where each object can appear at most once

A multiset is like a set but each element can appear multiple times; each element has a multiplicity. In Scala terms it is a bit like a Map[A, Int], from each element to its multiplicity

We can denote a multiset more compactly by using exponents to count how many times each element appears: \(\{2, 1, 1, 1, 1\} = \{2, 1^4\}\) and \(\{1, 1, 1, 1, 1, 1\} = \{1^6\}\)

Each multiset corresponds to a template for our hand; there are many possible ways to fill in this template by choosing different suits and choosing which ranks get paired up (or later tripled up)

I call these templates degeneracy classes because different templates have different repeated (or degenerate) ranks

Three different hands, all of shape {2, 1^4}

One Pair: Rank Sets

\(\{2, 1^4\}\)

We only care about the number of rank sets that contain a straight. As with the 5 card case we only have 5 distinct ranks here so we have \(S = 10\).

Write it \(S(r)\), with \(r\) the number of distinct ranks: \(S(5) = 10\)

The pair hand, then its rank set: forget suits, forget the repeat

One Pair: Placements

\(\{2, 1^4\}\)

A placement says which rank plays which role (i.e. which rank gets to be a pair, which gets to be a triple, etc). We can let \(P(d)\) denote the total placements for a degeneracy class \(d\).

Which rank gets the pair? 5 choices: \(P = 5\)

Or: the distinct orderings of \(\{2, 1, 1, 1, 1\}\) along the five ranks. 2 1 1 1 1, 1 2 1 1 1, … five of them

the pair

The 3 to 7 straight of diamonds with the pair on the 3, then the 4, 5, 6 and 7

One Pair: Suits

The six ways to suit a pair of 3s: spade-heart, spade-club, spade-diamond, heart-club, heart-diamond, club-diamond

Which 2 of the 4 suits does the pair get? 6 ways

Written \(\binom{4}{2}\), "4 choose 2": the number of ways to choose 2 things from 4

Each single picks 1 suit of 4: \(\binom{4}{1} = 4\) ways

\(Su = \binom{4}{2}\binom{4}{1}^4 = 6 \times 256 = 1{,}536\)

\(S(5) \times P \times Su = 10 \times 5 \times 1{,}536 = 76{,}800\)

Binomial Coefficients

♠ ♥ ♣ ♦
✓ ✓ ✗ ✗
✓ ✗ ✓ ✗
✓ ✗ ✗ ✓
✗ ✓ ✓ ✗
✗ ✓ ✗ ✓
✗ ✗ ✓ ✓

\(\binom{4}{2}\) is the number of distinct orderings of {✓, ✓, ✗, ✗} along the four suits

Four different things line up in \(4! = 4 \times 3 \times 2 \times 1 = 24\) ways

Swapping the two ✓s, or the two ✗s, changes nothing: divide by \(2!\) twice

\(\binom{4}{2} = \dfrac{4!}{2!\,2!} = \dfrac{24}{4} = 6\)

In general \(\binom{n}{k} = \dfrac{n!}{k!\,(n-k)!}\)

\((x+y)^4 = x^4 + 4x^3y + 6x^2y^2 + 4xy^3 + y^4\): the 6 is the orderings of \(x\,x\,y\,y\)

And \(\binom{52}{8} = 752{,}538{,}150\): every 8-card hand.

Six Distinct Ranks

\(\{1^6\}\), no duplicates

Either a run of six ranks: 9 of them

Or a 5-card straight plus a rank that doesn't "touch" it: 62

\(S(6) = 9 + 62 = 71\)

\(P = 1\),   \(Su = \binom{4}{1}^6\)

\(71 \times 4{,}096 = 290{,}816\)

Top: 3 to 8 of diamonds, a six-card straight. Bottom: 3 to 7 of diamonds plus a 9, laid out by rank with the 8 empty

Where the 62 Comes From

Top: 4 to 8 of diamonds on a full Ace-to-King rank line with the 3 and 9 crossed out. Bottom: Ace to 5 of diamonds with only the 6 crossed out

A 5-card straight, plus a sixth rank that doesn't "touch" it

8 straights sit in the middle: \(13 - 5 - 2 = 6\) free ranks each

2 straights end on an Ace: \(13 - 5 - 1 = 7\) free ranks each

\(8 \times 6 + 2 \times 7 = 62\)

All 71 of Them

All 71 six-rank rank sets containing a straight: the 9 six-card runs down the left, and the 62 five-card straights plus a sixth rank in ten rows, one per straight, the sixth rank tinted yellow

Six Cards, Counted

class multiset \(r\) \(S(r)\) \(P(d)\) \(Su(d)\) product
no duplicates \(\{1^6\}\) 6 71 \(\binom{6}{6} = 1\) \(\binom{4}{1}^{6} = 4{,}096\) 290,816
one pair \(\{2^{1}, 1^4\}\) 5 10 \(\binom{5}{1} = 5\) \(\binom{4}{2}\binom{4}{1}^{4} = 1{,}536\) 76,800
sum 367,616

Every hand is now built exactly once, the degeneracy classes can't ever overlap

The Insight

An eight-card hand, then the same hand with suits removed, then its rank set with the repeated 3 collapsed
Whether or not a hand contains a straight depends only on the set of ranks in that hand

Everything else, the suits and the placements, is just counting how many different hands share that rank set

That's why \(S(r)\) needs nothing but \(r\)

And there are only \(2^{13} = 8{,}192\) possible rank sets since each rank either appears or it doesn't. This is far fewer than the total number of hands

Degeneracy Classes for Eight Cards

3 to 7 of diamonds plus 9, Jack, King
\(\{1^{8}\}\)
no duplicates
3 to 7 of diamonds plus a second 3, 9 and Jack
\(\{2, 1^{6}\}\)
one pair
3 to 7 of diamonds plus a second 3 and 4, and a 9
\(\{2^{2}, 1^{4}\}\)
two pairs
3 to 7 of diamonds plus two more 3s and a 9
\(\{3, 1^{5}\}\)
three of a kind
3 to 7 of diamonds plus a second 3, 4 and 5
\(\{2^{3}, 1^{2}\}\)
three pairs
3 to 7 of diamonds plus two more 3s and a second 4
\(\{3, 2, 1^{3}\}\)
full house
3 to 7 of diamonds plus three more 3s
\(\{4, 1^{4}\}\)
four of a kind

One Hand, Four Choices

The shape {3, 2, 1^3} as blank cards: a column of three, a column of two, three singles
1. Choose a degeneracy class: (e.g. \(\{3, 2, 1^3\}\))
The rank set 3 4 5 6 7
2. Choose a rank set that contains a straight (e.g. 3 to 7)
The triple on the 3 in yellow, the pair on the 4 in pink, singles on 5, 6, 7
3. Choose a placement (e.g. 3 gets to be a triple, 4 gets to be a pair)
The hand 3 3 3 4 4 5 6 7 with suits
4. Choose suits for every card

A General Approach

  1. Choose a degeneracy class, \(d\)
  2. Choose a rank set that contains a straight: \(S(r)\) ways
  3. Place the card counts on those ranks: \(P(d)\) ways
  4. Choose the suits: \(Su(d)\) ways
  5. Every hand comes from exactly one chain of choices, and since the choices after fixing the degeneracy class are independent we can just multiply them! \(S(r) \times P(d) \times Su(d)\)
  6. Sum over the degeneracy classes

$$\text{hands with a straight} = \sum_{d} S(r)\, P(d)\, Su(d)$$

Maths or Code?

We can derive a formula

I was unable to derive a formula so used Scala

  • \(P(d)\), the placements
  • \(Su(d)\), the suits
  • Listing every degeneracy class \(d\) for a hand size
  • \(S(r)\), the rank sets with a straight

Placements

\(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffffff}{\mathbf{1}}^3\}\)

the triple   the pair

\(P(d)\) is the number of distinct orderings of \(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffffff}{\mathbf{1}}, \textcolor{#ffffff}{\mathbf{1}}, \textcolor{#ffffff}{\mathbf{1}}\}\) along the ranks 3 to 7

\(P(d) = \dfrac{5!}{\textcolor{#fcecb0}{\mathbf{1!}}\,\textcolor{#ffd6da}{\mathbf{1!}}\,\textcolor{#ffffff}{\mathbf{3!}}} = \dfrac{120}{6} = 20\)

Another way to think about this would be to consider which rank gets to be a triple (5 choices) and then which rank gets to be a pair (4 choices.) But we shall see that the "orderings" approach is more generalisable

All 20 placements: one row per triple rank, the pair moving along the other four

Placements: a Harder One

\(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}^2, \textcolor{#ffffff}{\mathbf{1}}^2\}\)

the triple   the pairs

9 cards: a triple, two pairs and two singles, on the ranks 3 to 7

\(P(d)\) is the number of distinct orderings of \(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffffff}{\mathbf{1}}, \textcolor{#ffffff}{\mathbf{1}}\}\)

\(P(d) = \dfrac{5!}{\textcolor{#fcecb0}{\mathbf{1!}}\,\textcolor{#ffd6da}{\mathbf{2!}}\,\textcolor{#ffffff}{\mathbf{2!}}} = \dfrac{120}{4} = 30\)

5 choices for the triple, then \(\binom{4}{2} = 6\) for the two pairs: \(5 \times 6 = 30\)

All 30 placements of a triple, two pairs and two singles on the ranks 3 to 7; triple tinted yellow, pairs pink

Multinomial Coefficients

3 4 5 6 7
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\)
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\)
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\)
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\)
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\)
\(\textcolor{#fcecb0}{\mathbf{3}}\) \(\textcolor{#ffd6da}{\mathbf{2}}\) \(\textcolor{#ffffff}{\mathbf{1_C}}\) \(\textcolor{#ffffff}{\mathbf{1_B}}\) \(\textcolor{#ffffff}{\mathbf{1_A}}\)

ranks across the top, card counts below:
six line-ups, one placement

Label the singles A, B and C: \(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffffff}{\mathbf{1_A}}, \textcolor{#ffffff}{\mathbf{1_B}}, \textcolor{#ffffff}{\mathbf{1_C}}\}\) lines up in \(5! = 120\) ways

Shuffling the labels changes nothing: every placement shows up \(3! = 6\) times

\(P = \dfrac{5!}{\textcolor{#fcecb0}{\mathbf{1!}}\,\textcolor{#ffd6da}{\mathbf{1!}}\,\textcolor{#ffffff}{\mathbf{3!}}} = \dfrac{120}{6} = 20\)

That is a multinomial coefficient, \(\binom{5}{\textcolor{#fcecb0}{\mathbf{1}},\,\textcolor{#ffd6da}{\mathbf{1}},\,\textcolor{#ffffff}{\mathbf{3}}}\): the distinct orderings of a multiset

\((\textcolor{#fcecb0}{\boldsymbol{x}}+\textcolor{#ffd6da}{\boldsymbol{y}}+\textcolor{#ffffff}{\boldsymbol{z}})^5\): the coefficient of \(\textcolor{#fcecb0}{\boldsymbol{x}}\,\textcolor{#ffd6da}{\boldsymbol{y}}\,\textcolor{#ffffff}{\boldsymbol{z}}^3\) is \(\binom{5}{\textcolor{#fcecb0}{\mathbf{1}},\,\textcolor{#ffd6da}{\mathbf{1}},\,\textcolor{#ffffff}{\mathbf{3}}} = 20\), the orderings of \(\textcolor{#fcecb0}{\boldsymbol{x}}\,\textcolor{#ffd6da}{\boldsymbol{y}}\,\textcolor{#ffffff}{\boldsymbol{z}}\,\textcolor{#ffffff}{\boldsymbol{z}}\,\textcolor{#ffffff}{\boldsymbol{z}}\)

A binomial is just the two-bucket case: \(\binom{n}{k} = \binom{n}{k,\,n-k}\)

Multinomial Coefficients: a Harder One

\(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffd6da}{\mathbf{2}}, \textcolor{#ffffff}{\mathbf{1}}, \textcolor{#ffffff}{\mathbf{1}}\}\): a triple, two pairs and two singles

Label the pairs A and B, and the singles A and B: \(\{\textcolor{#fcecb0}{\mathbf{3}}, \textcolor{#ffd6da}{\mathbf{2_A}}, \textcolor{#ffd6da}{\mathbf{2_B}}, \textcolor{#ffffff}{\mathbf{1_A}}, \textcolor{#ffffff}{\mathbf{1_B}}\}\) lines up in \(5! = 120\) ways

Swapping the two pairs changes nothing: divide by \(\textcolor{#ffd6da}{\mathbf{2!}}\)

Swapping the two singles changes nothing either: divide by \(\textcolor{#ffffff}{\mathbf{2!}}\) again

\(P = \dfrac{5!}{\textcolor{#fcecb0}{\mathbf{1!}}\,\textcolor{#ffd6da}{\mathbf{2!}}\,\textcolor{#ffffff}{\mathbf{2!}}} = \dfrac{120}{4} = 30 = \binom{5}{\textcolor{#fcecb0}{\mathbf{1}},\,\textcolor{#ffd6da}{\mathbf{2}},\,\textcolor{#ffffff}{\mathbf{2}}}\)

\((\textcolor{#fcecb0}{\boldsymbol{x}}+\textcolor{#ffd6da}{\boldsymbol{y}}+\textcolor{#ffffff}{\boldsymbol{z}})^5\): the coefficient of \(\textcolor{#fcecb0}{\boldsymbol{x}}\,\textcolor{#ffd6da}{\boldsymbol{y}}^2\,\textcolor{#ffffff}{\boldsymbol{z}}^2\) is 30, the orderings of \(\textcolor{#fcecb0}{\boldsymbol{x}}\,\textcolor{#ffd6da}{\boldsymbol{y}}\,\textcolor{#ffd6da}{\boldsymbol{y}}\,\textcolor{#ffffff}{\boldsymbol{z}}\,\textcolor{#ffffff}{\boldsymbol{z}}\)

Multinomial Coefficients Formula

$$\binom{n}{k_{1}, k_{2}, \ldots, k_{m}} = \frac{n!}{k_{1}! k_{2}! \cdots k_{m}!}$$

The number of distinct orderings of a multiset of \(n\) elements, where \(k_i\) is the multiplicity of the \(i\)th element

so \(n = k_1 + k_2 + \cdots + k_m\)

The Suit Factor

\(\{3^1, 2^1, 1^3\}\)

Under each rank, every way to assign the suits

card count multiplicity suit factor
3 1 \(\binom{4}{3} = 4\)
2 1 \(\binom{4}{2} = 6\)
1 3 \(\binom{4}{1}^{3} = 64\)

\(Su(d) = \binom{4}{3}\binom{4}{2}\binom{4}{1}^3\)

\(= 4 \times 6 \times 64 = 1{,}536\)

The hand 3 3 3 4 4 5 6 7 with every suit choice listed under each rank: 4, 6, 4, 4, 4

A General Degeneracy Class

\(\{3, 2, 1^3\}\): card counts \(a_1, a_2, a_3 = 3, 2, 1\); multiplicities \(m(3) = 1,\ m(2) = 1,\ m(1) = 3\); \(t = 3\); \(r = 5\)

$$\{ a_{1}^{m(a_{1})} , a_{2}^{m(a_{2})} , \ldots , a_{t}^{m(a_{t})} \}$$

\(a_i\) how many cards of a rank, between 1 and 4; \(t\) distinct values
\(m(a_i)\) a function that gives the multiplicity of each element: how many ranks have \(a_i\) cards
\(r = \sum_{i=1}^{t} m(a_i)\) the number of distinct ranks

The Formulae

$$Su(\{a_1^{m(a_1)}, \ldots, a_t^{m(a_t)}\}) = \prod_{i=1}^{t} \binom{4}{a_i}^{m(a_i)}$$

$$\begin{aligned} P(\{a_1^{m(a_1)},a_2^{m(a_2)} \ldots, a_t^{m(a_t)}\}) &= \binom{r}{m(a_1),m(a_2), \ldots, m(a_t)} \\ &= \frac{r!}{m(a_1)!m(a_2)!\cdots m(a_t)!} \end{aligned}$$

Eight Cards

class multiset \(r\) \(S(r)\) \(P(d)\) \(Su(d)\)
no duplicates \(\{1^{8}\}\) 8 ? \(\binom{8}{8} = 1\) \(\binom{4}{1}^{8} = 65{,}536\)
one pair \(\{2^{1}, 1^{6}\}\) 7 ? \(\binom{7}{1,6} = 7\) \(\binom{4}{2}\binom{4}{1}^{6} = 24{,}576\)
two pairs \(\{2^{2}, 1^{4}\}\) 6 71 \(\binom{6}{2,4} = 15\) \(\binom{4}{2}^{2}\binom{4}{1}^{4} = 9{,}216\)
three of a kind \(\{3^{1}, 1^{5}\}\) 6 71 \(\binom{6}{1,5} = 6\) \(\binom{4}{3}\binom{4}{1}^{5} = 4{,}096\)
three pairs \(\{2^{3}, 1^{2}\}\) 5 10 \(\binom{5}{3,2} = 10\) \(\binom{4}{2}^{3}\binom{4}{1}^{2} = 3{,}456\)
full house \(\{3^{1}, 2^{1}, 1^{3}\}\) 5 10 \(\binom{5}{1,1,3} = 20\) \(\binom{4}{3}\binom{4}{2}\binom{4}{1}^{3} = 1{,}536\)
four of a kind \(\{4^{1}, 1^{4}\}\) 5 10 \(\binom{5}{1,4} = 5\) \(\binom{4}{4}\binom{4}{1}^{4} = 256\)

We have a "nice" formula for two columns but not for \(S\) sadly.

A cat

The "Algorithm" in Scala

$$\text{hands with a straight} = \sum_{d} S(r)\, P(d)\, Su(d)$$

Integer Partitions of 8

An integer partition of 8 writes 8 as a sum of positive whole numbers, order ignored

class partition of 8 multiset \(r\)
no duplicates \(1{+}1{+}1{+}1{+}1{+}1{+}1{+}1\) \(\{1^{8}\}\) 8
one pair \(2{+}1{+}1{+}1{+}1{+}1{+}1\) \(\{2^{1}, 1^{6}\}\) 7
two pairs \(2{+}2{+}1{+}1{+}1{+}1\) \(\{2^{2}, 1^{4}\}\) 6
three of a kind \(3{+}1{+}1{+}1{+}1{+}1\) \(\{3^{1}, 1^{5}\}\) 6
three pairs \(2{+}2{+}2{+}1{+}1\) \(\{2^{3}, 1^{2}\}\) 5
full house \(3{+}2{+}1{+}1{+}1\) \(\{3^{1}, 2^{1}, 1^{3}\}\) 5
four of a kind \(4{+}1{+}1{+}1{+}1\) \(\{4^{1}, 1^{4}\}\) 5

There are actually 22 partitions of 8 but only 7 of them are relevant to us.

Parts of at most 4, because there are 4 suits. At least 5 parts, because a straight needs 5 ranks

Degeneracy Classes, in Scala

\(\{2^2, 1^4\}\) is Map(2 -> 2, 1 -> 4): card count to multiplicity

:| is iron: a refined Int the compiler knows is in range

Partitions of 6, Recursively

The recursion tree of integerPartitions(6, maxPart = 4): each branch picks the next part, no bigger than the one before; nine leaves, of which 1+1+1+1+1+1 and 2+1+1+1+1 are green

Each branch picks the next part, never bigger than the one before; maxPart = 4 rules out 5 and 6

● at least 5 parts, kept   ● dropped by the filter

The two survivors are the two six-card templates: \(\{1^6\}\) and \(\{2, 1^4\}\)

From Parts to a Multiset

List(2, 2, 1, 1, 1, 1) becomes Map(2 -> 2, 1 -> 4)

groupMapReduce: group equal parts, map each to 1, add up the 1s

Eight Cards, Seven Classes

This is what my Show instance (cats' type class for printing) spits out

Placements, in Scala

$$\begin{aligned} P(\{a_1^{m(a_1)},a_2^{m(a_2)} \ldots, a_t^{m(a_t)}\}) &= \binom{r}{m(a_1),m(a_2), \ldots, m(a_t)} \\ &= \frac{r!}{m(a_1)!m(a_2)!\cdots m(a_t)!} \end{aligned}$$

d.values are the multiplicities \(m(a_i)\), and they add up to \(r\)

The Suit Factor, in Scala

$$Su(\{a_1^{m(a_1)}, \ldots, a_t^{m(a_t)}\}) = \prod_{i=1}^{t} \binom{4}{a_i}^{m(a_i)}$$

Rank Sets With a Straight

Base case: the ten straights (allFiveRankStraights, next slide). Step: add any rank the set doesn't have; still a straight. Repeat.

The Ten Straights

Ten starting ranks, Ace to Ten. The % 13 wraps King round to Ace, so Ten gives T J Q K A. Nothing starts above Ten, so nothing wraps any further: no Q K A 2 3.

Rank Sets With a Straight: One Step

Two straights, 3 to 7 and 4 to 8, each extended by three extra ranks shown in yellow; the run 3 to 8 is built from both (pink) and appears once in the deduplicated set on the right

the added rank   built twice, but deduped because I'm using Set

Every straight, plus every rank it doesn't have: \(10 \times 8 = 80\) rank sets

Each of the 9 runs of six is built twice, so the Set keeps \(80 - 9 = 71 = S(6)\)

Rank Sets With a Straight, Counted

Bar chart of S(r): 10, 71, 217, 371, 384, 234, 77, 13, 1

10 and 71 match what we counted by hand. Not in the OEIS (the On-Line Encyclopedia of Integer Sequences), sadly.

Eight Cards, Again

class multiset \(r\) \(S(r)\) \(P(d)\) \(Su(d)\) product
no duplicates \(\{1^{8}\}\) 8 371 \(\binom{8}{8} = 1\) \(\binom{4}{1}^{8} = 65{,}536\) 24,313,856
one pair \(\{2^{1}, 1^{6}\}\) 7 217 \(\binom{7}{1,6} = 7\) \(\binom{4}{2}\binom{4}{1}^{6} = 24{,}576\) 37,330,944
two pairs \(\{2^{2}, 1^{4}\}\) 6 71 \(\binom{6}{2,4} = 15\) \(\binom{4}{2}^{2}\binom{4}{1}^{4} = 9{,}216\) 9,815,040
three of a kind \(\{3^{1}, 1^{5}\}\) 6 71 \(\binom{6}{1,5} = 6\) \(\binom{4}{3}\binom{4}{1}^{5} = 4{,}096\) 1,744,896
three pairs \(\{2^{3}, 1^{2}\}\) 5 10 \(\binom{5}{3,2} = 10\) \(\binom{4}{2}^{3}\binom{4}{1}^{2} = 3{,}456\) 345,600
full house \(\{3^{1}, 2^{1}, 1^{3}\}\) 5 10 \(\binom{5}{1,1,3} = 20\) \(\binom{4}{3}\binom{4}{2}\binom{4}{1}^{3} = 1{,}536\) 307,200
four of a kind \(\{4^{1}, 1^{4}\}\) 5 10 \(\binom{5}{1,4} = 5\) \(\binom{4}{4}\binom{4}{1}^{4} = 256\) 12,800
total: 9.82% 73,870,336

Run It

Hand size Total hands With a straight Probability
5 2,598,960 10,240 0.39%
6 20,358,520 367,616 1.81%
7 133,784,560 6,454,272 4.82%
8 752,538,150 73,870,336 9.82%
9 3,679,075,400 619,588,736 16.84%
10 15,820,024,220 4,051,217,344 25.61%
11 60,403,728,840 21,461,806,976 35.53%
12 206,379,406,870 94,674,009,184 45.87%
13 635,013,559,600 355,161,047,872 55.93%

Now With Gaps

$$\sum_{d} \textcolor{#f4c430}{S_g(r)}\, P(d)\, Su(d)$$

Only \(S\) knows what a straight is, so only \(S\) changes: \(S(r)\) becomes \(S_g(r)\), with \(g\) the biggest gap allowed

Normal straights correspond to \(S_0\). The Shortcut Joker corresponds to \(S_1\). \(P(d)\) and \(Su(d)\) remain the same

The recursion for \(S\) still works, so only its base case changes

Threading the Gap Through

Only the base case changes: allFiveRankStraights(maxGap) builds a tree of straights from every starting rank

Base Case for Normal Straights

Ten chains of five ranks, one per starting rank from Ace to 10

Start with the Ace low straight

And then generate the rest by increasing all ranks by one

Base Case for Gappy Straights

A binary tree from the Ace: at each step add 1 or 2 ranks; 16 leaves, each a gappy straight

From an Ace the next rank can be a 2 or a 3! Each time we select the next rank there are actually 2 choices

\(2^4 = 16\) Ace-low gappy straights

Base Case for Gappy Straights, in Scala

Start With Cards Other Than Ace

The same tree started from a 9: branches that pass a high Ace are cut off, leaving 5 straights

The same tree from every start

● a gappy straight   ● past the Ace, pruned

128 gappy straights, instead of 10

Fewer than the \(16 \times 10 = 160\) the tree suggests: trees that start high get pruned at the Ace

Every Starting Rank, in Scala

Add 1 to everything each round, kicking out anything past the high Ace, until nothing is left

Then flatten, and map the ordinals back to ranks: 128 gappy straights

Eight Cards, With Gaps

class multiset \(r\) \(S_1(r)\) \(P(d)\) \(Su(d)\) product
no duplicates \(\{1^{8}\}\) 8 1,156 \(\binom{8}{8} = 1\) \(\binom{4}{1}^{8} = 65{,}536\) 75,759,616
one pair \(\{2^{1}, 1^{6}\}\) 7 1,096 \(\binom{7}{1,6} = 7\) \(\binom{4}{2}\binom{4}{1}^{6} = 24{,}576\) 188,547,072
two pairs \(\{2^{2}, 1^{4}\}\) 6 576 \(\binom{6}{2,4} = 15\) \(\binom{4}{2}^{2}\binom{4}{1}^{4} = 9{,}216\) 79,626,240
three of a kind \(\{3^{1}, 1^{5}\}\) 6 576 \(\binom{6}{1,5} = 6\) \(\binom{4}{3}\binom{4}{1}^{5} = 4{,}096\) 14,155,776
three pairs \(\{2^{3}, 1^{2}\}\) 5 128 \(\binom{5}{3,2} = 10\) \(\binom{4}{2}^{3}\binom{4}{1}^{2} = 3{,}456\) 4,423,680
full house \(\{3^{1}, 2^{1}, 1^{3}\}\) 5 128 \(\binom{5}{1,1,3} = 20\) \(\binom{4}{3}\binom{4}{2}\binom{4}{1}^{3} = 1{,}536\) 3,932,160
four of a kind \(\{4^{1}, 1^{4}\}\) 5 128 \(\binom{5}{1,4} = 5\) \(\binom{4}{4}\binom{4}{1}^{4} = 256\) 163,840
total 366,608,384

Rank Sets With a Gappy Straight

\(r\) \(S_0(r)\) \(S_1(r)\)
5 10 128
6 71 576
7 217 1,096
8 371 1,156
9 384 709
10 234 286
11 77 78
12 13 13
13 1 1

The recursive step is the same, it's just that with gappy straights we have more to begin with!

The Answer

$$\frac{366{,}608{,}384}{752{,}538{,}150} = 48.72\%$$

The 50/50 logo

Without the joker it was about 10%, so having this joker makes getting a straight 5 times easier!!!

All of It

Hand size Gap 0 Gap 1 Gap 2 Gap 3 Gap 4
5 0.39% 5.04% 18.56% 33.69% 43.26%
6 1.81% 16.42% 41.95% 62.84% 74.68%
7 4.82% 31.99% 62.07% 80.36% 89.56%
8 9.82% 48.72% 76.83% 89.87% 95.71%
9 16.84% 63.94% 86.66% 94.89% 98.20%
10 25.61% 76.17% 92.73% 97.50% 99.23%
11 35.53% 85.07% 96.23% 98.82% 99.68%
12 45.87% 91.06% 98.13% 99.46% 99.87%
13 55.93% 94.86% 99.11% 99.76% 99.95%

Gap 0 is a normal straight. Gap 1 is the Shortcut Joker. Gaps 2 to 4 need a joker that doesn't exist yet

One Idea, Three Ways

Whether a hand contains a straight depends only on its rank set
A hand, the same hand with suits removed, and its rank set

$$\sum_{d} \textcolor{#f4c430}{S_g(r)}\, P(d)\, Su(d)$$

An Exercise for the Reader

The Four Fingers joker

Four Fingers

All Flushes and Straights can be made with 4 cards

Which parts of \(\sum_{d} S(r)\, P(d)\, Su(d)\) change?

What are the odds of a straight in 8 cards? And with the Shortcut Joker as well?

Thanks

The blog post: stanosphere.github.io/blog/counting-balatro-straights