23
\$\begingroup\$

Challenge

Bar Dice is a simple game played in a Bar with Dice (hence the name). You roll 5 six-sided dice and attempt to make the best hand.

Scoring is based on amassing the largest number of dice with the same digits. Each hand must include at least a single "Ace", or one, in order to be a valid hand; Aces act as "wilds", and can be paired with any other digit. The strength of a player's hand depends first on the number of digits and then value of those digits. As an example, a hand (counting wilds) with four 3's is better than a hand with three 5's, but not better than a hand with five 2's.
Taken from the Wikipedia article

This means the highest ranked hand is made entirely of 6's and 1's, and the lowest ranked is any hand without a 1.

Your challenge is to take two hands and return which player won, or if they tied.

Input

Two unsorted lists of 5 numbers, ranging from 1 to 6. Each list represents a player's hand. The input format is flexible.

Output

Any three distinct but consistent, static values (ranges are not allowed) signifying whether player 1 or player 2 won, or if it was a tie. Please state in your answer what values you are using for what. For example, you can return -1 if P1 wins, 0 if it's a tie, and 1 if P2 wins.

Rules

  • Input will always be valid
  • Only the best possible score of each hand is used to determine a winner. There are no tie-breakers. E.g., [1,4,4,3,3] will tie [1,4,4,2,2] instead of using the 3's and 2's as a tie-breaker.
  • Output must be one of the 3 chosen values every time. Simply mapping all negative numbers to P1 Wins is not allowed and must be normalized.
  • Invalid hands, i.e. those with no 1's, lose to all valid hands but tie with all other invalid hands. E.g., [2,2,2,2,2] ties [3,3,3,3,3].
  • A hand of [1,1,1,1,1] counts as a valid set of 6's for ranking purposes.
  • This is so shortest byte count wins.

Examples

#You guys are pretty good at finding edge-cases that break things. Good job!
Input:  [2,1,5,6,6], [6,2,6,6,6]
Output: P1 Wins

Input:  [2,4,5,6,6], [6,2,6,6,6]
Output: Tie

Input:  [1,2,3,4,5], [5,4,3,2,1]
Output: Tie

Input:  [1,5,5,3,2], [5,4,1,6,6]
Output: P2 Wins

Input:  [3,2,2,2,1], [4,1,3,6,6]
Output: P1 Wins

Input:  [1,1,1,1,1], [6,1,1,6,6]
Output: Tie

Input:  [1,3,3,4,4], [1,2,2,5,5]
Output: P2 Wins

Input:  [1,3,3,5,5], [1,3,3,2,2]
Output: P1 Wins

Input:  [1,3,3,3,4], [1,1,3,3,3]
Output: P2 Wins

Input:  [2,2,2,6,1], [5,3,3,1,2]
Output: P1 Wins

Input:  [5,5,5,1,5], [1,1,1,1,1]
Output: P2 Wins

Input:  [1,1,1,1,1], [1,1,5,1,1]
Output: P1 Wins
\$\endgroup\$

17 Answers 17

9
\$\begingroup\$

Jelly, 17 14 bytes

ċⱮ6Ḣ©+$®aĖUṀ)M

Try it online!

A monadic link that takes a list of the two lists as its argument and returns [1] for player 1 wins, [2] for player 2 wins and [1, 2] for a tie. TIO link tidies this up for display.

Thanks to @JonathanAllan for saving 3 bytes!

Explanation

            )   | For each input list (e.g. of one list 1,1,3,3,4)
ċⱮ6             | - Count each of 1..6 (e.g. 2,0,2,1,0,0)
      $         | - Following as a monad:
   Ḣ            |   - Head (number of 1s)
    ©︎           |   - Copy to register
     +          |   - Add (e.g. 2,4,3,0,0)
       ®a       | - Register logical and with this
         Ė      | - Enumerate (e.g. [1,2],[2,4],[3,3],[4,0],[5,0])
          U     | - Reverse each (e.g. [2,1],[4,2],[3,3],[0,4],[0,5])
            Ṁ   | - Max (e.g. 4,2)
              M | Index/indices of maximal score
\$\endgroup\$
  • 1
    \$\begingroup\$ You could replace the IṠ with M and output a list of the winner(s). \$\endgroup\$ – Jonathan Allan Jun 6 at 17:51
  • \$\begingroup\$ @JonathanAllan Good point! Thanks \$\endgroup\$ – Nick Kennedy Jun 6 at 17:54
  • 1
    \$\begingroup\$ 15 bytes by making use of the register. \$\endgroup\$ – Jonathan Allan Jun 6 at 18:08
  • 1
    \$\begingroup\$ I think the may now be redundant too since the lists sort the same as the integers. \$\endgroup\$ – Jonathan Allan Jun 6 at 18:14
  • 1
    \$\begingroup\$ This is a lovely approach. Well done. \$\endgroup\$ – Jonah Jun 7 at 14:44
9
\$\begingroup\$

R, 115 96 bytes

-6 bytes thanks to Giuseppe.

-6 bytes thanks to Aaron Hayman.

-2 bytes thanks to Arnauld, following the output format in his JavaScript answer.

function(i,j)(f(i)-f(j))/0
f=function(x,s=tabulate(x,6),l=s[1]+s[-1]*!!s[1])max(l)*6+order(l)[5]

Try it online!

Returns Inf for P1, NaN for a tie, -Inf for P2.

Uses the helper function f which computes a score for each hand. The score is defined as follows: let d be the digit which is repeated the most, and n the number of times it is repeated. Then the score is 6*n+d if there is at least one ace, and 0 if there are no aces. We then just need to find the player with the highest score.

Ungolfed:

f = function(x) {
  s = tabulate(x, 6)         # number of occurrences of each integer
  l = s[1] + s[-1] * !!s[1]  # add the number of wild aces to all values; set to 0 if s[1] == 0
  max(l) * 6 +               # highest number of repetitions (apart from aces)
    order(l)[5]              # most repeated integer (take largest in case of tie)
}
function(i, j){
  sign(f(i) - f(j))
}
\$\endgroup\$
  • \$\begingroup\$ You can use order(l)[5] instead of max.col(t(l),"l") to get a 96 byte solution: Try it online! \$\endgroup\$ – Aaron Hayman Jun 7 at 14:25
  • \$\begingroup\$ @AaronHayman Very nice, thanks! \$\endgroup\$ – Robin Ryder Jun 7 at 14:33
6
\$\begingroup\$

JavaScript (ES6),  97  90 bytes

Takes input as (a)(b). Returns +Infinity for P1, -Infinity for P2 or NaN for a tie.

a=>b=>((g=(x,m)=>x>6?m*/1/.test(a):g(-~x,a.map(k=>n+=k<2|k==x,n=x/6)|m>n?m:n))()-g(a=b))/0

Try it online!

Commented

a => b => (                 // a[] = dice of P1; b[] = dice of P2
  ( g = (                   // g is a recursive function taking:
      x,                    //   x = dice value to test; initially, it is either undefined
                            //       or set to a non-numeric value
      m                     //   m = maximum score so far, initially undefined
    ) =>                    //
      x > 6 ?               // if x is greater than 6:
        m * /1/.test(a)     //   return m, or 0 if a[] does not contain any 1 
      :                     // else:
        g(                  //   do a recursive call:
          -~x,              //     increment x (or set it to 1 if it's non-numeric)
          a.map(k =>        //     for each dice value k in a[]:
            n +=            //       add 1 to n if:
              k < 2 |       //         k is equal to 1
              k == x,       //         or k is equal to x
            n = x / 6       //       start with n = x / 6
          ) |               //     end of map()
          m > n ?           //     if m is defined and greater than n:
            m               //       pass m unchanged
          :                 //     else:
            n               //       update m to n
        )                   //   end of recursive call
  )()                       // first call to g, using a[]
  - g(a = b)                // subtract the result of a 2nd call, using b[]
) / 0                       // divide by 0 to force one of the 3 consistent output values
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 16 15 bytes

-1 byte thanks to JonathanAllan

εWΘ*6L¢ć+°ƶà}ZQ

Try it online!

Returns [1, 0] for P1 wins, [1, 1] for ties, [0, 1] for P2 wins.

Rather than using lexicographic order on a 2-tuple (dice count, dice value), this computes the score as 10**dice count * dice value. Hands without a 1 score 5.

ε           }           # map each hand to its computed score
 WΘ                     # minimum == 1 (1 if the hand contains a 1, 0 otherwise)
   *                    # multiply (sets hands without 1 to [0, 0, 0, 0, 0])
    6L                  # range [1..6]
      ¢                 # count occurences of each in the hand
       ć                # head extract (stack is now [2-count, ..., 6-count], 1-count)
        +               # add the 1-count to all the other counts
         °              # 10**x
          ƶ             # multiply each element by its 1-based index
           à            # take the maximum

Z                       # maximum
 Q                      # equality test (vectorizes)
\$\endgroup\$
  • 1
    \$\begingroup\$ Ohhh.. I like the ć+ (now that I see it I cannot believe I hadn't thought about it..)! That's so much better than what I was attempting.. I did had a similar idea with °. :) Except that I was already at 20 bytes and still had to fix an issue for test case [[1,1,1,1,1],] [6,1,1,6,6]].. So thanks for saving me time so I can put my attempt in the garbage bin.. ;p \$\endgroup\$ – Kevin Cruijssen Jun 6 at 13:55
  • 1
    \$\begingroup\$ @KevinCruijssen Yeah it's amazing how well ć+ works. My initial idea started with æʒW}ʒ1KË, but this gets killed by the [1,1,1,1,1] issue. \$\endgroup\$ – Grimy Jun 6 at 14:08
  • 1
    \$\begingroup\$ Yeah, my approach was along the lines of ε1¢©Āy{γéθ¬sg®+°P}`.S, but the [1,1,1,1,1] screwed that over as well indeed. Your entire answer got a nice synergy with the WΘ*, 6L¢, ć+, and °ƶ. Especially the builtins Wćƶ really show their strength here. \$\endgroup\$ – Kevin Cruijssen Jun 6 at 14:30
  • \$\begingroup\$ W isn't actually needed, 6L¢¬Ā* is the same byte-count as WΘ*6L¢. \$\endgroup\$ – Grimy Jun 6 at 14:38
  • \$\begingroup\$ Hmm, good point. :) Thought W without popping and then * showed it's strength, but ¬ without popping and then * is basically the same. The fact that it doesn't pop is the strength I was implying to, saving a byte. But it's indeed mainly ćƶ. \$\endgroup\$ – Kevin Cruijssen Jun 6 at 14:40
6
\$\begingroup\$

Python 2, 85 81 80 bytes

lambda p:cmp(*[max((h.count(i)+h.count(i>1),i)*(1in h)for i in[6]+h)for h in p])

Try it online!

Returns 1 for P1, 0 for tie, and -1 for P2.

-1 byte, thanks to squid

\$\endgroup\$
  • 1
    \$\begingroup\$ Whitespace between 1 and in can go \$\endgroup\$ – squid Jun 6 at 15:25
  • \$\begingroup\$ @squid Thanks, :) \$\endgroup\$ – TFeld Jun 7 at 6:49
4
\$\begingroup\$

Perl 6, 60 49 bytes

&[cmp]o*.map:{.{1}&&max (.{2..6}X+.{1})Z ^5}o&bag

Try it online!

Returns More, Same, Less for P1 Wins, Tie, P2 Wins.

Explanation

       *.map:  # Map input lists
                                             &bag  # Convert to Bag
             {                             }o  # Pass to block
              .{1}&&  # Return 0 if no 1s
                    max  # Maximum of
                         .{2..6}  # number of 2s,3s,4s,5s,6s
                                X+.{1}  # plus number of 1s
                        (             )Z     # zipped with
                                         ^5  # secondary key 0,1,2,3,4
&[cmp]o  # Compare mapped values
\$\endgroup\$
3
\$\begingroup\$

T-SQL query, 148 bytes

Using table variable as input

p: player

v: value for roll

DECLARE @ table(p int,v int)
INSERT @ values(1,5),(1,5),(1,5),(1,5),(1,5)
INSERT @ values(2,4),(2,3),(2,3),(2,1),(2,4)

SELECT sign(min(u)+max(u))FROM(SELECT(p*2-3)*(s+sum(sign(s)-1/v))*(s/5*5+v+30)u
FROM(SELECT*,sum(1/v)over(partition by p)s FROM @)c GROUP BY v,s,p)x

Try it online

Player 1 wins returns -1
Tie returns 0 
Player 2 wins returns 1
\$\endgroup\$
2
\$\begingroup\$

Jelly, 21 bytes

crushed before I even posted it by Nick Kennedy :)

’-oÆṃṀ$$Ạ?fÆṃ$L;$o5)M

A monadic Link accepting a list of players which outputs a list of (1-indexed) winners.

So P1 is [1], P2 is [2] and a tie is [1,2].

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Java 8, 244 240 236 215 bytes

a->b->{int c[][]=new int[2][7],m[]=new int[2],s,p,i=5;for(;i-->0;c[1][b[i]]++)c[0][a[i]]++;for(i=7;i-->2;)for(p=2;p-->0;m[p]=s>m[p]?s:m[p])s=c[p][1]>0?i+9*(c[p][i]+(i>1?c[p][1]:0)):0;return Long.compare(m[0],m[1]);}

-4 bytes thanks to @someone.
-21 bytes thanks to @Neil.

Returns 1 if P1 wins; -1 if P2 wins; 0 if it's a tie.

Try it online.

Explanation:

a->b->{                        // Method with 2 integer-array parameters & integer return
  int c[][]=new int[2][7],     //  Create a count-array for each value of both players,
                               //  initially filled with 0s
      m[]=new int[2],          //  The maximum per player, initially 0
      s,p,                     //  Temp-values for the score and player
  i=5;for(;i-->0;              //  Loop `i` in the range (5, 0]:
    c[1]                       //   For player 2:
        [b[i]                  //    Get the value of the `i`'th die,
             ]++)              //    and increase that score-count by 1
    c[0][a[i]]++;              //   Do the same for player 1
  for(i=7;i-->2;)              //  Then loop `i` in the range (7, 2]:
    for(p=2;p-->0              //   Inner loop `p` over both players:
        ;                      //     After every iteration:
         m[p]=s>m[p]?s:m[p])   //      Update the max score if the current score is higher
      s=                       //    Set the current score to:
        c[p][1]>0?             //     If at least one 1 is rolled:
          i                    //      Use the current value `i`
          +9*                  //      With 9 times the following added:
             (c[p][i]          //       The amount of dice for value `i`
              +(i>1?           //       And if the current die `i` is not 1:
                    c[p][1]:0))//        Add the amount of dice for value 1
        :0;                    //     Else (no 1s were rolled): the score is 0
  return Long.compare(m[0],m[1]);}
                               //  Finally compare the maximum scores of the players,
                               //  resulting in -1 if a<b; 0 if a==b; 1 if a>b
\$\endgroup\$
  • \$\begingroup\$ In the for loop over p, you can replace ...*(c[p][1]>0?1:0) with c[p][1]>0?...:0. I canot post a TIO link, as it's too long and I don't want to shorten it. The ungolfed version has unbalanced parentheses somewhere around there. \$\endgroup\$ – someone Jun 7 at 0:33
  • \$\begingroup\$ @someone Ah, of course, thanks. I added the c[p][1]>0? check later on as bug-fix, but apparently without thinking a lot. Thanks for the -4. :) \$\endgroup\$ – Kevin Cruijssen Jun 7 at 6:40
  • \$\begingroup\$ Why the *(i<2?6:i)? You're just duplicating effort for i=6 and i=1. This can be just *i (and stop looping when you get to 2). \$\endgroup\$ – Neil Jun 7 at 8:31
  • \$\begingroup\$ Also, the 9 can be any magic number between 5 and about 32, right? If you use 8 then instead of (int)Math.pow(8,(...)*i) you can use i<<3*(...). \$\endgroup\$ – Neil Jun 7 at 8:34
  • 1
    \$\begingroup\$ I ended up with a->b->{int c[][]=new int[2][7],m[]=new int[2],s,p,i=5;for(;i-->0;c[1][b[i]]++)c[0][a[i]]++;for(i=7;i-->2;)for(p=2;p-->0;m[p]=s>m[p]?s:m[p])s=c[p][1]>0?i+9*(c[p][i]+(i>1?c[p][1]:0)):0;return Long.compare(m[0],m[1]);} which seems to pass all of your test cases... \$\endgroup\$ – Neil Jun 7 at 8:52
2
\$\begingroup\$

PowerShell, 112 126 123 121 bytes

Takes input as (a)(b). Returns -1 for P1 win, 1 for P2 or 0 for a tie.

$args|%{$d=$($_-ne1|group|sort c*,n*|%{$m=$_};(7*(($o=($_-eq1).Count)+$m.Count)+$m.Name+6*!$m)[!$o])-$d}
[math]::Sign($d)

Try it online!

Test case @( @(1,1,5,1,1), @(1,1,1,1,1), 1) added.

Unrolled:

$args|%{
    $score=$(                                            # powershell creates a new scope inside expression $(...)
        $_-ne1|group|sort count,name|%{$max=$_}          # $max is an element with the widest group and the maximum digit except 1
        $ones=($_-eq1).Count                             # number of 1s in a player's hand
        $scoreRaw=7*($ones+$max.Count)+$max.Name+6*!$max # where $max.Name is digit of the widest group
        $scoreRaw[!$ones]                                # output $scoreRaw if the array contains 1, otherwise output $null 
    )                                                    # powershell deletes all variables created inside the scope on exit
    $diff=$score-$diff
}
[math]::Sign($diff)                                     # output the score difference
\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 78 75 74 bytes

-1 byte by Greg Martin

Order@@(#~FreeQ~1||Last@Sort[Reverse/@Tally@Flatten[#/. 1->Range@6]]&)/@#&

Try it online!

Outputs -1 when player 1 wins, 1 when player 2 wins, and 0 for a tie.

                                    Helper function to score a list:
FreeQ[#,1] ||                       If there are 0 1s, score is True
Last@Sort[                          Otherwise, take the largest element of
    Reverse/@Tally@                 the {frequency, number} pairs in the flat list
       Flatten[ #/. 1->Range@6]     where each 1 is replaced by {1,2,3,4,5,6}.
]&                              e.g. {1,3,3,5,5} -> {1,2,3,4,5,6,3,3,5,5} -> {3,5}

Order @@ (...) /@ #&                Apply this function to both lists,
                                    then find the ordering of the result.
\$\endgroup\$
  • \$\begingroup\$ You can save one byte by replacing FreeQ[#,1] with #~FreeQ~1. \$\endgroup\$ – Greg Martin Jun 8 at 19:38
1
\$\begingroup\$

Jelly, 27 bytes

’o5Rṗ5¤ṢŒr$€UẎṀ1e⁸¤×µ€_/Ṡo/

Try it online!

1 for P1, -1 for P2, 0 for tie

Explanation

’o5Rṗ5¤ṢŒr$€UẎṀ1e⁸¤×µ€_/Ṡo/  Main link
                    µ€       For each list
’                            Decrement each value (so 1s become falsy)
 o                           Vectorized logical or (this replaces previous 1s (now 0s) with the test values)
  5Rṗ5¤                      1..5 cartesian-power 5 (1,1,1,1,1; 1,1,1,1,2; 1,1,1,1,3; ...)
          $€                 For each test list
       ṢŒr                   Sort and run-length encode (gives [digit, #digit])
            U                Reverse each list (gives [#digit, digit])
             Ẏ               Tighten by one (gives a list containing each possible hand for each possible wildcard)
              Ṁ              Take the maximum
               1e⁸¤×         Multiply the list values by (whether or not the original contained a 1) - becomes [0, 0] if not
                      _/Ṡ    Take the sign of the difference between the #digits and the digits
                         o/  If the number of digits differs, then 1/-1 is returned; otherwise, check the value of the digit (could still be 0)
\$\endgroup\$
1
\$\begingroup\$

Sledgehammer 0.4, 27 bytes

⢱⢙⢂⠠⡾⢃⠐⢈⠸⣞⠴⠻⠎⡥⡳⡐⢒⠘⢛⣩⡓⣮⡕⡠⣢⣡⠿

Decompresses into this Wolfram Language function:

Order @@ (FreeQ[#1, 1] || Last[Sort[Reverse[Tally[Flatten[#1 /. 1 -> Range[6]]], 2]]] & ) /@ #1 & 

which turns out to be exactly the same as my Mathematica answer.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 48 45 bytes

UMθ⮌E⁷№ι∨λ¹UMθ׬¬⊟ι⁺⊟ιιUMθ⟦⌈ι±⌕ι⌈ι⟧I⁻⌕θ⌈θ⌕θ⌊θ

Try it online! Link is to verbose version of code. Takes input as an array of arrays and outputs -1 if player 1 wins, 0 for a tie, and 1 if player 2 wins. Explanation:

UMθ⮌E⁷№ι∨λ¹

Replace each hand with the count of how many times the values 6..1 appear in the hand. The list is reversed because a) it makes it easier to find the highest value with the highest count and b) it makes it easier to remove the count of 1s. The count of 1s is doubled because it needs to be removed twice, once to check that it is nonzero and once to add it to the other counts.

UMθ׬¬⊟ι⁺⊟ιι

Add the count of 1s to the counts for 6..2, but set all of the counts to zero if the count of 1s was zero.

UMθ⟦⌈ι±⌕ι⌈ι⟧

For each hand find the highest count and the highest value with that count. (Actually we find value minus 6 as that's golfier.)

I⁻⌕θ⌈θ⌕θ⌊θ

Determine which hand won by subtracting the positions of the winning and losing hands. (If the hands are tied then the first hand is both winning and losing so the result is 0 as desired.)

\$\endgroup\$
1
\$\begingroup\$

C (gcc) / 32 bit, 117 bytes

t;m;s(int*r){int c[6]={};for(m=0;t=*r++;m=t>m?t:m)c[--t]+=5,t=t?c[t]+t:5;t=*c?*c+m:0;}f(a,b){t=s(a)-s(b);t=(t>0)-!t;}

Try it online!

Takes two zero-terminated integer arrays. Returns 1, 0, -1 for P1 Wins, P2 Wins, Tie.

\$\endgroup\$
  • 1
    \$\begingroup\$ @Veskah OK, fixed. \$\endgroup\$ – nwellnhof Jun 7 at 18:12
1
\$\begingroup\$

J, 47 44 bytes

*@-&([:({.([:>./#\@]+9^*@[*+)}.)1#.i.@6=/<:)

Try it online!

Inspired by Nick Kennedy's idea.

ungolfed

*@-&([: ({. ([: >./ #\@] + 9 ^ *@[ * +) }.) 1 #. i.@6 =/ <:)
\$\endgroup\$
1
\$\begingroup\$

Perl 5 -MList::Util=max -pl, 80 bytes

sub t{$t=pop;$_=max map$_ x$t=~s/$_//g,2..6;/./;$t&&$_.$t*($&||6)}$_=t($_)<=>t<>

Try it online!

Input:

Each player on a separate line, no spaces

Output:

1 Line one wins

0 Tie

-1 Line two wins

\$\endgroup\$
  • 1
    \$\begingroup\$ Changed based on your clarification of the rules of the game \$\endgroup\$ – Xcali Jun 10 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.