Skip to content

Instantly share code, notes, and snippets.

@harshavardhan
Created May 24, 2020 12:08
Show Gist options
  • Save harshavardhan/52cfa8c55e0a2a27f32f251147d965c7 to your computer and use it in GitHub Desktop.
Save harshavardhan/52cfa8c55e0a2a27f32f251147d965c7 to your computer and use it in GitHub Desktop.
Code to count number of distinct ranked poker hands
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rb pop_back
#define fr(i,a,b) for(int i=a;i <= b;i++)
#define fd(i,a,b) for(int i=a;i >= b;i--)
// cards suit
/*
clubs = 1,
diamonds = 2,
hearts = 3,
spades = 4
*/
// cards value
/*
two = 2,
three = 3,
four = 4,
five = 5,
six = 6,
seven = 7,
eight = 8,
nine = 9,
ten = 10,
jack = 11,
queen = 12,
king = 13,
ace = 14
*/
#define vt vector< pair<int,int> >
map< vt,int> st;
int cnt = 0;
bool verify = false;
bool debug = false;
void add(vt temp,int rank) {
if(verify) assert(temp.size() == 5);
// replace 1 with 14 for ace
for(int i=0;i < temp.size();i++) {
if(temp[i].second == 1) temp[i].second = 14;
}
if(verify) {
sort(temp.begin(),temp.end());
assert(st.count(temp) == 0);
}
if(debug) {
for(int i=0;i < temp.size();i++) {
cout << temp[i].first << "," << temp[i].second << " ";
}
cout << endl;
}
st[temp] = rank;
cnt++;
}
int main() {
int rank = 1;
// royal flush
fr(i,0,3) {
vt temp;
temp.pb(mp(i,10));
temp.pb(mp(i,11));
temp.pb(mp(i,12));
temp.pb(mp(i,13));
temp.pb(mp(i,14));
add(temp,rank);
}
rank++;
assert(rank-1 == 1);
// straight flush
fd(j,13,5) {
fr(i,0,3) {
vt temp;
temp.pb(mp(i,j-4));
temp.pb(mp(i,j-3));
temp.pb(mp(i,j-2));
temp.pb(mp(i,j-1));
temp.pb(mp(i,j));
add(temp,rank);
}
rank++;
}
assert(rank-1 == 10);
// four of a kind
fd(j,14,2) {
fd(k,14,2) {
if(k == j) continue;
fr(i,0,3) {
vt temp;
temp.pb(mp(i,k));
temp.pb(mp(0,j));
temp.pb(mp(1,j));
temp.pb(mp(2,j));
temp.pb(mp(3,j));
add(temp,rank);
}
rank++;
}
}
assert(rank-1 == 166);
// full house
fd(j,14,2) {
fd(k,14,2) {
if(k == j) continue;
vt temp;
fr(p,0,3) {
temp.pb(mp(p,j));
fr(q,p+1,3) {
temp.pb(mp(q,j));
fr(r,q+1,3) {
temp.pb(mp(r,j));
fr(s,0,3) {
temp.pb(mp(s,k));
fr(t,s+1,3) {
temp.pb(mp(t,k));
add(temp,rank);
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
rank++;
}
}
assert(rank-1 == 322);
// flush
fd(p,14,2) {
fd(q,p-1,2) {
fd(r,q-1,2) {
fd(s,r-1,2) {
fd(t,s-1,2) {
// ignore straight flush
if(p == q+1 and q == r+1 and r == s+1 and s == t+1) continue;
// ignore royal flush
if(p == 14 and q == 5 and r == 4 and s == 3 and t == 2) continue;
fr(i,0,3) {
vt temp;
temp.pb(mp(i,p));
temp.pb(mp(i,q));
temp.pb(mp(i,r));
temp.pb(mp(i,s));
temp.pb(mp(i,t));
add(temp,rank);
}
rank++;
}
}
}
}
}
assert(rank-1 == 1599);
// straight
fd(j,14,5) {
fr(p,0,3) {
fr(q,0,3) {
fr(r,0,3) {
fr(s,0,3) {
fr(t,0,3) {
if(p == q and q == r and r == s and s == t) continue;
vt temp;
temp.pb(mp(p,j-4));
temp.pb(mp(q,j-3));
temp.pb(mp(r,j-2));
temp.pb(mp(s,j-1));
temp.pb(mp(t,j));
add(temp,rank);
}
}
}
}
}
rank++;
}
assert(rank-1 == 1609);
// three of a kind
fd(j,14,2) {
fd(k,14,2) {
if(k == j) continue;
fd(l,k-1,2) {
if(l == j) continue;
fr(i,0,3) {
vt temp;
if(i != 0) temp.pb(mp(0,j));
if(i != 1) temp.pb(mp(1,j));
if(i != 2) temp.pb(mp(2,j));
if(i != 3) temp.pb(mp(3,j));
fr(a,0,3) {
temp.pb(mp(a,k));
fr(b,0,3) {
temp.pb(mp(b,l));
add(temp,rank);
temp.rb();
}
temp.rb();
}
}
rank++;
}
}
}
assert(rank-1 == 2467);
// double pair
fd(j,14,2) {
fd(k,j-1,2) {
fd(l,14,2) {
if(l == j or l == k) continue;
vt temp;
fr(p,0,3) {
temp.pb(mp(p,j));
fr(q,p+1,3) {
temp.pb(mp(q,j));
fr(r,0,3) {
temp.pb(mp(r,k));
fr(s,r+1,3) {
temp.pb(mp(s,k));
fr(t,0,3) {
temp.pb(mp(t,l));
add(temp,rank);
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
rank++;
}
}
}
assert(rank-1 == 3325);
// pair
fd(j,14,2) {
fd(p,14,2) {
fd(q,p-1,2) {
fd(r,q-1,2) {
if(j == p or j == q or j == r) continue;
vt temp;
fr(a,0,3) {
temp.pb(mp(a,j));
fr(b,a+1,3) {
temp.pb(mp(b,j));
fr(c,0,3) {
temp.pb(mp(c,p));
fr(d,0,3) {
temp.pb(mp(d,q));
fr(e,0,3) {
temp.pb(mp(e,r));
add(temp,rank);
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
rank++;
}
}
}
}
assert(rank-1 == 6185);
// high card
fd(p,14,2) {
fd(q,p-1,2) {
fd(r,q-1,2) {
fd(s,r-1,2) {
fd(t,s-1,2) {
// ignore straight
if(p == q+1 and q == r+1 and r == s+1 and s == t+1) continue;
if(p == 14 and q == 5 and r == 4 and s == 3 and t == 2) continue;
vt temp;
fr(a,0,3) {
temp.pb(mp(a,p));
fr(b,0,3) {
temp.pb(mp(b,q));
fr(c,0,3) {
temp.pb(mp(c,r));
fr(d,0,3) {
temp.pb(mp(d,s));
fr(e,0,3) {
// ignore flush
if(a == b and b == c and c == d and d == e) continue;
temp.pb(mp(e,t));
add(temp,rank);
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
temp.rb();
}
rank++;
}
}
}
}
}
assert(rank-1 == 7462);
rank--;
assert(rank == 7462 and cnt == 2598960);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment