Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3
  ignore  |  Print  
  Name that Poker hand - Tiny Code Challenge  (Read 26051 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #30 - Posted 2014-10-07 11:38:50 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #31 - Posted 2014-10-07 11:52:41 »

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 Grin
https://www.hackerrank.com/challenges/fizzbuzz/leaderboard/filter/language=java

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

Got ya! 6.60 Pointing
Got ya again! 6.80 Pointing

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


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #32 - Posted 2014-10-07 12:07:12 »

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

Anyway, here's the Poker hand:

1  
2  
3  
4  
5  
6  
7  
8  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 379
Projects: 2
Exp: 2 years


Resident Crazyman


« Reply #33 - Posted 2014-10-07 12:15:14 »

This thread has officially broken my brain. Thanks guy. Wink

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
LIVE-STREAMING DEVELOPMENT: http://www.hitbox.tv/rayvolution
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #34 - Posted 2014-10-07 12:29:06 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
   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;
   }

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


You broke it Emo

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #35 - Posted 2014-10-07 12:53:25 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
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;
}
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #36 - Posted 2014-10-07 12:54:50 »

1  
2  
-   v=[302]
+   v=y[302]
this aint no stinkin' javascript Wink

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #37 - Posted 2014-10-07 12:59:10 »

1  
2  
-   v=[302]
+   v=y[302]
this aint no stinkin' javascript Wink

 Huh Don't know how that got in...
Well 404, syntax not found.
Offline richierich
« Reply #38 - Posted 2014-10-07 13:37:47 »

Just have to say I strongly disapprove of this kind of game ... oh go on then Cheesy

1  
2  
-   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;}

Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #39 - Posted 2014-10-07 13:43:29 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #40 - Posted 2014-10-07 14:01:21 »

Back down to 394, thanks mostly to richierich  Pointing

EDIT: new record: 392! verified for all permutations.
1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g){
   int n=99,i=-2,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)if((p=n)<2)s=14;else{r=p=p<n?p:n;s=q=q>n?q:n;}
   for(w=q-p<5|s-r<5?7:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)w&=y[n]/2-1;
   for(v=y[302]>>5;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;
}
Offline richierich
« Reply #41 - Posted 2014-10-07 14:48:06 »

This could work

1  
2  
- s-r<5?w&v:0,
+ r>9?w&v:0,     //lowest card is > 9

Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #42 - Posted 2014-10-07 14:54:47 »

This could work

1  
2  
- s-r<5?w&v:0,
+ r>9?w&v:0,     //lowest card is > 9



1  
2  
3  
2H 3H 4H 5H 6H
   Riven: royal flush
   Richy: straight flush


Seems like that fixes a bug in my impl... Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline richierich
« Reply #43 - Posted 2014-10-07 15:12:28 »

Totally intentional of course!

Edit: I have to say your boolean-array-within-for-loop thing to replace a long sequence of if/else tests is great. Evil, but great  Tongue
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #44 - Posted 2014-10-07 17:11:16 »

It's getting pretty good now.

Verified against the "r > 9"

371 chars
1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g){
   int n=99,i=-2,p=n,q=-p,r=p,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)p=y[n]>0?n<2?n:(q=q>n?q:n)-q+(r=p<n?p:n):p;
   for(w=q-p<5|r>9?7:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)w&=y[n]/2-1;
   for(v=y[302]>>5;new int[]{r>9?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;
}
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #45 - Posted 2014-10-07 17:40:00 »

369 chars:
1  
2  
3  
4  
5  
6  
7  
8  
9  
int h(char[]g)
{
   int n=99,i=-2,p=n,q=-p,r=p,v,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)p=y[n]>0?n<2?n:(q=q>n?q:n)-q+(r=p<n?p:n):p;
   for(i=q-p<5|r>9?7:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)i&=y[n]/2-1;
   for(v=y[302]>>5;new int[]{r>9?i&v:0,i&v,y[202],y[399]&p,v,i,y[201],p-1,p,1}[99-n]<1;n--)p=y[398];
   return n;
}

So lame Tongue

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #46 - Posted 2014-10-07 18:14:35 »

Better:

358 chars
1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g){
   int n=99,i=-2,p=n,q=-p,r=p,v,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)q=y[n]>0&&(p=n)>1?q>(r=p)?q:n:q;
   for(i=q-p<5|r>9?7:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)i&=y[n]/2-1;
   for(v=y[302]>>5;new int[]{r>9?i&v:0,i&v,y[202],y[399]&p,v,i,y[201],p-1,p,1}[99-n]<1;n--)p=y[398];
   return n;
}


Just so happens that p is never less than n. (in the 2nd loop)
Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #47 - Posted 2014-10-07 19:59:41 »

I stared and stared and stared and I can neither reduce FizzBuzz (how did you do it Riven?!?) nor this 'h' method.

Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #48 - Posted 2014-10-07 21:09:20 »

If you post your FizzBuzz solution, I might give you a few hints.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #49 - Posted 2014-10-07 21:11:37 »

I WILL NOT BE DENIED

Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #50 - Posted 2014-10-07 21:12:31 »

Just give me time.... and money. Mostly money, that'd be nice.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #51 - Posted 2014-10-07 21:13:44 »

Just enough to bribe someone at Oracle to put an easteregg function in the stdlib. Then wait until next release.
Offline ags1

JGO Kernel


Medals: 356
Projects: 7


Make code not war!


« Reply #52 - Posted 2014-10-07 21:15:23 »

Code like this gets people fired, and rightly so!

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #53 - Posted 2014-10-07 21:17:05 »

Or they realize they can't fire you!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #54 - Posted 2014-10-07 21:17:35 »

Screwing up occasionally? You get fired.
Screwing up consistently? You've got job security.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #55 - Posted 2014-10-07 21:51:53 »

Yay! I matched Riven's score on FizzBuzz. Grin

Edit: CRAP. I matched BurntPizza.

Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #56 - Posted 2014-10-07 21:58:38 »

I hate you all.

My solution:
1  
class S{public static void main(String[]a){for(int i=0;++i<101;)System.out.println(i%3<1?i%5<1?"FizzBuzz":"Fizz":i%5<1?"Buzz":i);}}


EDIT: Aha, I just figured out I could use <1 instead of ==0, new score of 6.70!
EDIT2: Just shaved off another character: 6.80!

Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #57 - Posted 2014-10-07 22:01:23 »

Hint: You can cut off a few characters by switching the logic and using > instead of ==.

Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #58 - Posted 2014-10-07 22:09:09 »

Hah, I just realized that right before you posted it Grin

Offline richierich
« Reply #59 - Posted 2014-10-07 22:09:38 »

358 chars
1  
2  
3  
4  
5  
6  
7  
8  
int h(char[]g){
   int n=99,i=-2,p=n,q=-p,r=p,v,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)q=y[n]>0&&(p=n)>1?q>(r=p)?q:n:q;
   for(i=q-p<5|r>9?7:0;++n<99;y[297+y[99+n]]=n,y[396+y[n]]++)i&=y[n]/2-1;
   for(v=y[302]>>5;new int[]{r>9?i&v:0,i&v,y[202],y[399]&p,v,i,y[201],p-1,p,1}[99-n]<1;n--)p=y[398];
   return n;
}

Different algorithm - not sure how to count but this is about 40 chars fewer than the most recent one. Possibly buggy but handles the official test data so this way seems viable at least.

About 320:
1  
2  
3  
4  
5  
6  
7  
int h(char[]g) {
  int i=0,f=0,k=0,s=0,p=0,t=0,q=0,n=0;
  for(;i<17;i++){n=0;for(char x:g)if(x=="A23456789TJQKCDHS".charAt(i))n++;
  if(i<13){if(n>0&&i==12)k++;if(n==1)s++;else if(s<5)s=1;if(n==2)p++;if(n==3)t++;if(n==4)q++;}
  else if(n==5)f=1;}if(s<5)s=0;for(i=0;new int[]{f&s&k,f&s,q,t&p,f,s,t,p-1,p,1}[i]<=0;i++);
  return 99-i;
}


Now I feel all dirty Tongue


Pages: 1 [2] 3
  ignore  |  Print  
 
 

 
xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (117 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (260 views)
2017-08-02 12:28:51

cybrmynd (252 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (882 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!