Name that Poker hand - Tiny Code Challenge

Although I admire your effort, we don’t want no stinking exceptions :slight_smile: I’ll try to build on your new solution though!

Great, I just had to append a dummy [icode]true[/icode] value to the boolean[] (w is known to be false as otherwise the loop would have been escaped a few iterations earlier, so !w is true)


//for(boolean h:new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&i)>0,v,w,c[3]>0,i>1,i>0}){if(h)break;n--;}
//for(j=0;!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&i)>0,v,w,c[3]>0,i>1,i>0}[j++];n--);
  for(j=0;!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&i)>0,v,w,c[3]>0,i>1,i>0,!w}[j++];n--);


	int h(char[]g){
		int n=99,i=-2,j,p=n,q=-p,r=p,s=q,y[][]=new int[5][n];int[]a=y[0],c=y[2],e=y[4];
      for(;i<13;a[" A23456789TJQK".indexOf(g[i+=2])]++,y[1][g[++i]]++);
      for(i=n;i-->0;c[a[i]]=i)if(a[i]>0){p=p<i?p:i;q=q>i?q:i;j=i<2?14:i;r=r<j?r:j;s=s>j?s:j;}
      boolean v,w=q-p==4|s-r==4;for(i=n;i-->0;y[3][y[1][i]]=i,e[a[i]]++)w&=a[i]<2;v=y[3][5]>0;i=e[2];
      for(j=0;!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&i)>0,v,w,c[3]>0,i>1,i>0,!w}[j++];n--);
      return n;
	}

Code size: 439 bytes!

Ah, was about to post, but you beat my patch by 1 byte :point:
Edit, now 2. I was looking for that.

Thanks for a hilarious night :slight_smile: For now, at 1:30AM, my humble bed beckons. :point:

Loop rework, -3 bytes:

i just finished being a loop counter, so it’s value is known.


//i=e[2];
//for(j=0;!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&i)>0,v,w,c[3]>0,i>1,i>0,!w}[j++];n--);
  p=e[2];
  for(;!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&p)>0,v,w,c[3]>0,p>1,p>0,!w}[++i];n--);

Edit: Yay! Achievement get: medal from Riven!
Dream of code, friend :wink:

Too easy :-*


  for(p=e[2];!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&p)>0,v,w,c[3]>0,p>1,p>0,!w}[++i];n--);


	int h(char[]g){
		int n=99,i=-2,j,p=n,q=-p,r=p,s=q,y[][]=new int[5][n];int[]a=y[0],c=y[2],e=y[4];
      for(;i<13;a[" A23456789TJQK".indexOf(g[i+=2])]++,y[1][g[++i]]++);
      for(i=n;i-->0;c[a[i]]=i)if(a[i]>0){p=p<i?p:i;q=q>i?q:i;j=i<2?14:i;r=r<j?r:j;s=s>j?s:j;}
      boolean v,w=q-p==4|s-r==4;for(i=n;i-->0;y[3][y[1][i]]=i,e[a[i]]++)w&=a[i]<2;v=y[3][5]>0;
      for(p=e[2];!new boolean[]{w&v&s-r<5,w&v,c[4]>0,(e[3]&p)>0,v,w,c[3]>0,p>1,p>0,!w}[++i];n--);
      return n;
	}

Code size: 435!

Hah.
I too should probably sleep.

O_O

Topic reopened, for the purpose of getting my ass kicked, in the hope to return the favor. Bring it on, BurntPizza!

Sweet, Riven I beat your score on that FizzBuzz Code-Golf! Now to somehow figure out how to get to #1.

Never mind, I just broke the longtime scores of EVERYBODY and got a new previously-never-achieved Java score of 6.40 ;D
https://www.hackerrank.com/challenges/fizzbuzz/leaderboard/filter/language=java

BurntPizza has a version that reduced the code size from 435 to 394, so, I’m awaiting his publication (before attempting to smash it!)

https://www.hackerrank.com/challenges/fizzbuzz/leaderboard/filter/language=java

Got ya! 6.60 :point:
Got ya again! 6.80 :point:

Wildly interesting brag-fact: I started from scratch, took 10 minutes and submitted my first second attempt 8)

Damn, nicely done there. Feels good though, doesn’t it.

Anyway, here’s the Poker hand:

int h(char[]g){
	int n=99,i=-2,j,p=n,q=-p,r=p,s=q,v,w,y[]=new int[5*n];
	for(;i<13;y[n+g[++i]]++)y[" A23456789TJQK".indexOf(g[i+=2])]++;
	for(;n-->0;y[198+y[n]]=n)if(y[n]>0){p=p<n?p:n;q=q>n?q:n;j=n<2?14:n;r=r<j?r:j;s=s>j?s:j;}
	for(w=5-p+q|5-s+r;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)w&=y[n]/2-1;
	for(v=y[302];new int[]{s-r<5?w&v:0,w&v,y[202],y[399]&p,v,w,y[201],p-1,p,1}[99-n]<1;n--)p=y[398];
	return n;
}

Purely running with what we had earlier, any significant gains to be had probably require a re-design.

This thread has officially broken my brain. Thanks guy. :wink:

	public static void main(String[] args)
	{
		String[] hands = new String[9];
		hands[0] = "3H 5D JS 3C 7C"; // one pair
		hands[1] = "JH 4C 2C JD 2H"; // two pair
		hands[2] = "7H 3S 7S 7D 7C"; // four of a kind
		hands[3] = "8C 3H 8S 8H 3S"; // full house
		hands[4] = "8C 3C 4S 8C 3C"; // two pair
		hands[5] = "4C 6C 8S 5C 7C"; // straight
		hands[6] = "AC 2C 3C 4C 5C"; // straight flush: A,2,3,4,5 (ace low)
		hands[7] = "AC KC QC JC TC"; // royal flush: T,J,Q,K,A (ace high)
		hands[8] = "AC 8C QA JH TC"; // high card

		String[] name = new String[10];
		name[0] = "royal flush";
		name[1] = "straight flush";
		name[2] = "four of a kind";
		name[3] = "full house";
		name[4] = "flush";
		name[5] = "straight";
		name[6] = "three of a kind";
		name[7] = "two pair";
		name[8] = "one pair";
		name[9] = "high card";

		for(String hand : hands)
		{
			System.out.println(hand + ": " + name[99 - hRiven(hand.toCharArray())]);
			System.out.println(hand + ": " + name[99 - hBurnt(hand.toCharArray())]);

			if(hRiven(hand.toCharArray()) != hBurnt(hand.toCharArray()))
				throw new IllegalStateException();
		}

		System.out.println("----");

		char[] fullhand = new char[5 * 2 + 4];
		Arrays.fill(fullhand, ' ');
		permutations(new int[5], fullhand, 0);
	}

	private static void permutations(int[] is, char[] fullhand, int q)
	{
		outer: for(int i = 0; i < 52; i++)
		{
			for(int k = 0; k < q; k++)
				if(i == is[k])
					continue outer;
			is[q] = i;

			final int c = (i % 13);
			final int s = (i / 13);

			fullhand[q * 3 + 0] = "A23456789TJQK".charAt(c);
			fullhand[q * 3 + 1] = "HCSD".charAt(s);

			if(q < 4)
			{
				if(q == 1)
					System.out.println((is[0] + 1) + "/52, " + (is[1] + 1) + "/52");

				permutations(is, fullhand, q + 1);
			}
			else
			{
				if(hRiven(fullhand) != hBurnt(fullhand))
				{
					System.out.println(new String(fullhand));
					String oRiven = name[99 - hRiven(fullhand)];
					String oBurnt = name[99 - hBurnt(fullhand)];
					System.out.println("\tRiven: " + oRiven);
					System.out.println("\tBurnt: " + oBurnt);
					throw new IllegalStateException();
				}
			}
		}
	}

	static int hRiven(char[] g)
	{
		int n = 99, i = -2, j, p = n, q = -p, r = n, s = q;
		int[][] y = new int[5][n];
		int[] a = y[0], b = y[1], c = y[2], d = y[3], e = y[4];
		for(; i < 13; a[" A23456789TJQK".indexOf(g[i += 2])]++, b[g[++i]]++)
			;
		for(i = n; --i >= 0; c[a[i]] = i)
			if(a[i] > 0)
			{
				p = p < i ? p : i;
				q = q > i ? q : i;
				j = i == 1 ? 14 : i;
				r = r < j ? r : j;
				s = s > j ? s : j;
			}
		for(i = n; --i >= 0; d[b[i]] = i, e[a[i]]++)
			;
		boolean u, v, w = q - p == 4 | s - r == 4;
		for(int f : a)
			w &= f < 2;
		v = d[5] > 0;
		u = w & v;
		for(boolean h : new boolean[] { u & s - r < 5, u, c[4] > 0, e[3] == 1 & e[2] == 1, v, w, c[3] > 0, e[2] == 2, e[2] == 1 })
		{
			if(h)
				break;
			n--;
		}
		return n;
	}

	static int hBurnt(char[] g)
	{
		int n = 99, i = -2, j, p = n, q = -p, r = p, s = q, v, w, y[] = new int[5 * n];
		for(; i < 13; y[n + g[++i]]++)
			y[" A23456789TJQK".indexOf(g[i += 2])]++;
		for(; n-- > 0; y[198 + y[n]] = n)
			if(y[n] > 0)
			{
				p = p < n ? p : n;
				q = q > n ? q : n;
				j = n < 2 ? 14 : n;
				r = r < j ? r : j;
				s = s > j ? s : j;
			}
		for(w = 5 - p + q | 5 - s + r; ++n < 99; y[297 + y[99 + n]] = n, y[396 + y[n]]++)
			w &= y[n] / 2 - 1;
		for(v = y[302]; new int[] { s - r < 5 ? w & v : 0, w & v, y[202], y[399] & p, v, w, y[201], p - 1, p, 1 }[99 - n] < 1; n--)
			p = y[398];
		return n;
	}


AH 2H 3H 4H 6H
	Riven: flush
	Burnt: straight flush

You broke it :emo:

Ah, I was afraid of that. Should have wrote the exhaustive test.

Deopt to 404 for now, I’ll see about it when I get back from class.

int h(char[]g){
	int n=99,i=-2,j,p=n,q=-p,r=p,s=q,v,w,y[]=new int[5*n];
	for(;i<13;y[n+g[++i]]++)y[" A23456789TJQK".indexOf(g[i+=2])]++;
	for(;n-->0;y[198+y[n]]=n)if(y[n]>0){p=p<n?p:n;q=q>n?q:n;j=n<2?14:n;r=r<j?r:j;s=s>j?s:j;}
	for(w=q-p<5|s-r<5?1:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)w&=y[n]/2-1;
	for(v=y[302]>0?1:0;new int[]{s-r<5?w&v:0,w&v,y[202],y[399]&p,v,w,y[201],p-1,p,1}[99-n]<1;n--)p=y[398];
	return n;
}

-   v=[302]
+   v=y[302]

this aint no stinkin’ javascript :wink:

??? Don’t know how that got in…
Well 404, syntax not found.

Just have to say I strongly disapprove of this kind of game … oh go on then :smiley:


-   p=p<n?p:n;p=q>n?q:n;j=n<2?14:n;r=r<j?r:j;s=s>j?s:j;
+   if(n<2){p=1;s=14;}else{r=p=p<n?p:n;s=q=q>n?q:n;}

Which also frees the localvar ‘j’, shaving off 2 more bytes: 399!