Who won a Game of Bar Dice?

24

1

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

Veskah

Posted 2019-06-06T12:46:31.390

Reputation: 3 580

Answers

10

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

Nick Kennedy

Posted 2019-06-06T12:46:31.390

Reputation: 11 829

1You could replace the IṠ with M and output a list of the winner(s). – Jonathan Allan – 2019-06-06T17:51:16.777

@JonathanAllan Good point! Thanks – Nick Kennedy – 2019-06-06T17:54:30.267

115 bytes by making use of the register. – Jonathan Allan – 2019-06-06T18:08:30.083

1I think the may now be redundant too since the lists sort the same as the integers. – Jonathan Allan – 2019-06-06T18:14:53.107

1This is a lovely approach. Well done. – Jonah – 2019-06-07T14:44:13.590

9

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))
}

Robin Ryder

Posted 2019-06-06T12:46:31.390

Reputation: 6 625

You can use order(l)[5] instead of max.col(t(l),"l") to get a 96 byte solution: Try it online!

– Aaron Hayman – 2019-06-07T14:25:25.750

@AaronHayman Very nice, thanks! – Robin Ryder – 2019-06-07T14:33:51.533

6

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

TFeld

Posted 2019-06-06T12:46:31.390

Reputation: 19 246

1Whitespace between 1 and in can go – Reinstate Monica – 2019-06-06T15:25:19.057

@squid Thanks, :) – TFeld – 2019-06-07T06:49:03.053

6

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)

Grimmy

Posted 2019-06-06T12:46:31.390

Reputation: 12 521

1Ohhh.. 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 – Kevin Cruijssen – 2019-06-06T13:55:38.043

1@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. – Grimmy – 2019-06-06T14:08:21.273

1Yeah, 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. – Kevin Cruijssen – 2019-06-06T14:30:20.197

W isn't actually needed, 6L¢¬Ā* is the same byte-count as WΘ*6L¢. – Grimmy – 2019-06-06T14:38:22.860

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 ćƶ. – Kevin Cruijssen – 2019-06-06T14:40:53.760

6

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

Arnauld

Posted 2019-06-06T12:46:31.390

Reputation: 111 334

4

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

nwellnhof

Posted 2019-06-06T12:46:31.390

Reputation: 10 037

4

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

t-clausen.dk

Posted 2019-06-06T12:46:31.390

Reputation: 2 874

2

Java 8, 244 240 236 215 199 bytes

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

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

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
      p,                       //  Temp-value for the 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=14;i-->4;)             //  Then loop `i` in the range (14, 4]:
    m[p=i%2]=                  //   Set the score of a player to:
                               //   (even `i` = player 1; odd `i` = player 2)
      Math.max(                //    The max between:
        m[p],                  //     The current value,
                               //     And the value we calculate as follows:
        c[p][1]>0?             //     If this player rolled at least one 1:
          i/2                  //      Use the current value `i` integer-divided by 2
          +9*                  //      With 9 times the following added:
             (c[p][i/2]        //       The amount of dice for value `i//2`
              +c[p][1])        //       Add the amount of dice for value 1
        :                      //     Else (no 1s were rolled):
         0);                   //      Use 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

Kevin Cruijssen

Posted 2019-06-06T12:46:31.390

Reputation: 67 575

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. – my pronoun is monicareinstate – 2019-06-07T00:33:55.937

@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. :) – Kevin Cruijssen – 2019-06-07T06:40:39.150

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). – Neil – 2019-06-07T08:31:17.357

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*(...). – Neil – 2019-06-07T08:34:25.270

Or just i+9*(...) seems like it should work. – Neil – 2019-06-07T08:42:17.217

@Neil As for your first comment regarding *(i<2?6:i) and looping in the range (7, 2], that would fail for test case [1,1,1,1,1], ..., which should count as five 6s. As for the (int)Math.pow(8,c[p][i]+(i>1?c[p][1]:0))*... going to (c[p][i]+(i>1?c[p][1]:0)<<3)*..., it seems to give an incorrect result for test case [3,2,2,2,1],[4,1,3,6,6]. – Kevin Cruijssen – 2019-06-07T08:48:18.627

1I 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... – Neil – 2019-06-07T08:52:53.527

@Neil Ah, of course. I was using the power because I multiple with i, but if I just add the i instead I can use the 9* instead of 9**. Thanks for -21 bytes! – Kevin Cruijssen – 2019-06-07T08:59:39.527

2

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!

Jonathan Allan

Posted 2019-06-06T12:46:31.390

Reputation: 67 804

2

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

mazzy

Posted 2019-06-06T12:46:31.390

Reputation: 4 832

2

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.

lirtosiast

Posted 2019-06-06T12:46:31.390

Reputation: 20 331

You can save one byte by replacing FreeQ[#,1] with #~FreeQ~1. – Greg Martin – 2019-06-08T19:38:35.517

1

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)

HyperNeutrino

Posted 2019-06-06T12:46:31.390

Reputation: 26 575

1

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.)

Neil

Posted 2019-06-06T12:46:31.390

Reputation: 95 035

1

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.

lirtosiast

Posted 2019-06-06T12:46:31.390

Reputation: 20 331

1

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.

nwellnhof

Posted 2019-06-06T12:46:31.390

Reputation: 10 037

1@Veskah OK, fixed. – nwellnhof – 2019-06-07T18:12:47.793

1

J, 47 44 bytes

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

Try it online!

Inspired by Nick Kennedy's idea.

ungolfed

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

Jonah

Posted 2019-06-06T12:46:31.390

Reputation: 8 729

1

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

Xcali

Posted 2019-06-06T12:46:31.390

Reputation: 7 671

1Changed based on your clarification of the rules of the game – Xcali – 2019-06-10T00:52:14.870