Inspired and looted from this dice challenge by Arnauld

Input

You are given a 5x1 or 1x5 (your choice) dice matrix which consist of binary 3x3 sub-matrices.

Goal

Given a valid dice matrix, you are to score it using the rules of 6,5,4 which are as follows:

  • If the roll contains 6,5,4, add the other two dice together and that is your score. E.g. 4,X,5,6,Y = X+Y
  • Otherwise, the score is 0. E.g. 5,5,5,4,1 = 0

Wiki article for the game

Dice patterns

$$\begin{align} &1:\pmatrix{\color{gray}0,\color{gray}0,\color{gray}0\\\color{gray}0,1,\color{gray}0\\\color{gray}0,\color{gray}0,\color{gray}0} &&2:\pmatrix{1,\color{gray}0,\color{gray}0\\\color{gray}0,\color{gray}0,\color{gray}0\\\color{gray}0,\color{gray}0,1}\text{or}\pmatrix{\color{gray}0,\color{gray}0,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,\color{gray}0,\color{gray}0}\\ &3:\pmatrix{1,\color{gray}0,\color{gray}0\\\color{gray}0,1,\color{gray}0\\\color{gray}0,\color{gray}0,1}\text{or}\pmatrix{\color{gray}0,\color{gray}0,1\\\color{gray}0,1,\color{gray}0\\1,\color{gray}0,\color{gray}0} &&4:\pmatrix{1,\color{gray}0,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,\color{gray}0,1}\\ &5:\pmatrix{1,\color{gray}0,1\\\color{gray}0,1,\color{gray}0\\1,\color{gray}0,1} &&6:\pmatrix{1,\color{gray}0,1\\1,\color{gray}0,1\\1,\color{gray}0,1}\text{or}\pmatrix{1,1,1\\\color{gray}0,\color{gray}0,\color{gray}0\\1,1,1} \end{align}$$

Rules

  • The matrix is guaranteed to only contain valid faces but will include the 2,3 and 6 permutations. You can also take it in either orientation in whatever way is convenient. Please state the chosen orientation in your answer.
  • Output the calculated score
  • Standard Loopholes are forbidden
  • This is .

Examples

// 2,5,2,4,6: Output should be: 4
[ [ 0,0,1 ],
  [ 0,0,0 ],
  [ 1,0,0 ], 
  [ 1,0,1 ],
  [ 0,1,0 ],
  [ 1,0,1 ], 
  [ 0,0,1 ],
  [ 0,0,0 ],
  [ 1,0,0 ], 
  [ 1,0,1 ],
  [ 0,0,0 ],
  [ 1,0,1 ], 
  [ 1,1,1 ],
  [ 0,0,0 ],
  [ 1,1,1 ] ]

// 1,6,2,4,6: Output should be: 0
[ [ 0,0,0, 1,0,1, 1,0,0, 1,0,1, 1,1,1  ],
  [ 0,1,0, 1,0,1, 0,0,0, 0,0,0, 0,0,0  ],
  [ 0,0,0, 1,0,1, 0,0,1, 1,0,1, 1,1,1  ] ]

// 5,6,6,4,6: Output should be: 12
[ [ 1,0,1, 1,0,1, 1,1,1, 1,0,1, 1,1,1  ],
  [ 0,1,0, 1,0,1, 0,0,0, 0,0,0, 0,0,0  ],
  [ 1,0,1, 1,0,1, 1,1,1, 1,0,1, 1,1,1  ] ]

// 3,3,4,5,6: Output should be: 6
[ [ 0,0,1, 1,0,0, 1,0,1, 1,0,1, 1,1,1  ],
  [ 0,1,0, 0,1,0, 0,0,0, 0,1,0, 0,0,0  ],
  [ 1,0,0, 0,0,1, 1,0,1, 1,0,1, 1,1,1  ] ]

// 2,5,2,5,6: Output should be: 0
[ [ 0,0,1, 1,0,1, 1,0,0, 1,0,1, 1,1,1  ],
  [ 0,0,0, 0,1,0, 0,0,0, 0,1,0, 0,0,0  ],
  [ 1,0,0, 1,0,1, 0,0,1, 1,0,1, 1,1,1  ] ]
  • Suggested test case: One where the value 5 is present two times, like [2,5,2,5,6]. My current solution works for all four of your test cases (by using a very bad method of sorting the values and removing the sub-list [4,5,6]), which of course fails when 5 is present two times. – Kevin Cruijssen Aug 8 at 6:46
  • 6
    The core idea is good, however the way it has been drafted strikes me as falling under one of our categories of things to avoid when writing challenges, namely "adding unnecessary fluff". Parsing the dice does not seem to be the main part of the challenge, but it may well take half the code. – Jonathan Allan Aug 8 at 7:09
  • 1
    @JonathanAllan It's a game about dice so I gave them dice. I do agree that making them validate faces would be fluffy hence it's not part of the challenge. Dice matrices also allow for interesting solutions because just scoring 654 with integers is not very hard or all that unique. – Veskah Aug 9 at 20:15
  • 1
    @JonathanAllan I'll keep it in mind for the future but won't change the specs now. – Veskah Aug 9 at 20:34
  • 1
    @Veskah yes definitely don't change the specs now! Here's to many more fun challenges :) – Jonathan Allan Aug 9 at 20:36

15 Answers 15

05AB1E, 15 bytes

3ôO<O©3LsK_P®O*

Try it online! or Check out a test suite!

Uses the same trick as Chas Brown and Lynn use: decrementing each integer in each 3x3 sub-matrix rather than subtracting 15 at the end. Expects input in column format.

How it works

3ôO<OD3LsK_PsO* – Full program.
3ô              – Split the input in 3x3 matrices representing dice faces.
  O<O           – Sum each, subtract one and sum each again.
                  This works because after the first O there are 3 elements in each
                  list and there are 5 lists in total, so 3 * 5 = 15 = 4 + 5 + 6.
     ©          – Copy this to the register.
      3LsK      – Push [1, 2, 3] and perform set subtraction. In this scenario, we
                  have already subtracted 3 from each list, so [1, 2, 3] represent
                  the actual dice values [4, 5, 6]. If the resulting list is empty,
                   that means that those values do exist in our roll. Therefore:
          _P    – Produce a list of zeros of that length and take the product
                  (4,5,6 isn't in the dice roll if the list is empty 
                  and this method assures that in this case the product is 1, else 0)
            ®O* – Sum what's in the register and multiply by that.

MATL, 12 bytes

9es3-I:ymp*s

Try it online!

Takes input in horizontal orientation as a 3x15 matrix. @Chas Brown's trick of subtracting 3 early (instead of 15 later) saved multiple bytes in different ways.

9e      % reshape input to have 9 rows - each dice matrix is linearized into a column
s       % sum each column (to get dice values)
3-      % subtract 3 from each value, let's call this array A
I:      % form range 1 to 3
y       % bring a copy of array A to the top of stack
m       % check if each of 1, 2, 3 are members of A
        %  returns a logical array of 3 boolean values
p       % product of that result - 0 if any were not members, 1 if they all were
*       % multiply the original array A by this result 
s       % sum 

Jelly, 14 bytes

s3§’§µ3Rœ-⁸ṆaS

Try it online!

Accepts a column of dice.

                  With the input (a 15×3 binary matrix):
s3                 Split into threes: now we have 5 dice, each a 3×3 matrix.
  §                Sum-each. Now we have a list of triples.
   ’               Decrement: turn each triple [x,y,z] into [x−1,y−1,z−1].
    §              Sum-each. Now we have a list of dice pip sums minus 3.
     µ            With these sums X:
      3Rœ-⁸        Subtract them from {1, 2, 3} as a multiset.
           Ṇ       Is the result empty? (This means {1, 2, 3} ⊆ X.)
            aS     If so, yield sum(X). Else the result stays 0.

Just like Chas Brown’s Python answer, this offsets each dice value by −3 so that we don’t need to subtract 15 (4+5+6) from the very last sum.

Python 2, 81 bytes

f=lambda a,r=[]:a and f(a[3:],r+[sum(sum(a[:3],[]))-3])or({1,2,3}<=set(r))*sum(r)

Try it online!

All input is expected to be in the column form.

Perl 6, 48 46 bytes

Thanks to Ramillies for -2 bytes

{(^3+4⊂.flat.rotor(9)>>.sum)*(.flat.sum-15)}

Try it online!

An anonymous code block that takes the matrix vertically and returns an integer.

Explanation:

{                                          } # Anonymous code block
                             (.flat.sum-15)  # Get the total sum of the array minus 15
                            * # Multiply by:
 (^3+4⊂                    )    # Whether 4,5,6 is a sub-array of:
       .flat.rotor(9)>>.sum     # The value of each dice
  • Your alt version is now unequal to your current byte count, Jo – Veskah Aug 8 at 2:28
  • 1
    I think you can save 1 byte if you take one List of Lists instead of a bunch of Arrays and use .flat instead of .[*;*], like this – Ramillies Aug 8 at 8:35

Brachylog, 23 22 bytes

+ᵐ-₁ᵐḍ₅+ᵐJo⊇~⟦₁3∧J+|∧0
  • +3 bytes but prints out 0 instead of false if no 4,5,6 (Fatalize)
  • -2 4 bytes (sundar)

One of my first branchylog programs. Can probably be golfed more. Prints false if there is no 4,5,6. idk how to make it output 0.

Try it online!

  • 1
    I haven't read the challenge, but if you want your program to output 0 instad of false, appending |∧0 at the end should do the job. – Fatalize Aug 8 at 12:56
  • 1
    I think you can save 2 bytes by postponing the ḍ₅: Try it online! – sundar Aug 8 at 13:01
  • 1
    2 more bytes can be saved by using the ~ operator and avoiding needing I: Try it online! (note that I have also added the |∧0 here). – sundar Aug 8 at 13:06

R, 56 bytes

function(m)sum(x<-by(c(m),0:44%/%9,sum)-3)*all(1:3%in%x)

Try it online!

  • -6 bytes thanks to JayCe
  • -1 byte thanks to Giuseppe
  • this is awesome ! save 3 bytes – JayCe Aug 10 at 14:34
  • also I think you can do c(m) to flatten m instead of m[1:45] – JayCe Aug 10 at 14:40
  • @JayCe: of course, thanks a lot ! – digEmAll Aug 10 at 15:48
  • 56 bytes using inline assignment – Giuseppe Aug 10 at 18:37

Dyalog APL, 28 27 bytes

{+/⍵×∧/∨/⍉⍵∘.=⍳3}¯3+(+/¨,¨)

Try it online!

Takes a 1x5 matrix of 3x3 dice matrices as input.

+/¨,¨ adds up the pip values of each of the dice. Then subtract 3, use ∨/⍉⍵∘.=⍳3 to check if there is at least one instance each of (1, 2, 3), AND the results together with ∧/ and multiply the result (0 or 1) by the sum of the adjusted dice values (+/⍵).

Retina 0.8.2, 45 bytes

¶

M!`.{9}
%M`1
O`
¶

G`4.*5.*6
.
$*
1{15}

1

Try it online! Takes vertical dice. Explanation:

¶

M!`.{9}

Reshape the dice into 5 individual rows.

%M`1

Get the values of each row.

O`

Sort them into order.

Join them into a single string.

G`4.*5.*6

Ensure that the three required dice are present.

.
$*

Convert each die to unary.

1{15}

Subtract the matching 4, 5 and 6.

1

Sum and convert to decimal.

Pyth, 20 bytes

*-ssQ15}j456TySsMc5s

Expects input as a column of dice (as in test case 1). Try it online here, or verify all test cases at once here.

*-ssQ15}j456TySsMc5sQ   Implicit: Q=eval(input()), T=10
                        Trailing Q inferred
                   sQ   Flatten input into a single array
                 c5     Chop into 5 pieces
               sM       Take the sum of each piece
              S         Sort
             y          Take the power set
       }                Does the above contain...
        j456T           ... [4,5,6]? 1 if so, 0 otherwise <a>
  ssQ                   Deep sum of input (total pips)
 -   15                 Subtract 15 <b>
*                       Multiply <a> and <b>, implicit print

05AB1E, 30 29 22 bytes

3ôOOJ456vyõ.;}Dg<iSOë0

Takes the dice-matrices below each other.

Try it online or verify all test cases.

Explanation:

3ô              # Split into sub-lists of 3, so we now have our dices
                #  i.e. [[A],[B],[C],[D],[E],[F],...] → [[[A],[B],[C]],[[D],[E],[F]],...]
  OO            # Sum each row, and then sum the total
                #  i.e. [[0,0,1],[0,0,0],[1,0,0]] → [1,0,1] → 2
    J           # Join everything together to a single string
                #  [5,4,2,5,6] → '54256'
     456v    }  # For each `y` in [4,5,6]:
         yõ.;   #  Replace the first occurrence with an empty string
                #  i.e. '54256' → '52'
D               # Duplicate the result
 g              # Take the length
                #  i.e. '52' → 2
  <i            # If the length is exactly 2 (and thus we've successfully removed [4,5,6]):
    SO          #  Split the string into digits again, and sum them as result
  ë             # Else:
    0           #  The result is 0 instead

JavaScript (ES6), 78 bytes

Takes input as a column of dice.

a=>!(a+0).replace(/.{18}/g,s=>(t+=n=s.split`1`.length,a|=1<<n),t=-20)|a>223&&t

Try it online!

How?

By doing a + 0, we implicitly flatten and coerce the input array to a string and add a trailing "0", which gives exactly 5x18 = 90 characters.

For instance, the first test case leads to:

0,0,1,0,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,1,1,1,0,0,0,1,1,10
\____dice #1____/ \____dice #2____/ \____dice #3____/ \____dice #4____/ \____dice #5____/

For each dice substring s of 18 characters, we compute the number n of pips + 1 and we update the total number of pips t with:

t += n = s.split`1`.length

We re-use the input array a as a bitmask to keep track of each dice that was encountered at least once:

a |= 1 << n

If the roll contains at least one 4, one 5 and one 6, the bitmask a will have the following bits set:

11100000 (224 in decimal)

We test this by doing a > 223. If successful, we return t. Because we count one extra pip for each dice and because we don't want to count 4+5+6 in the result, t is initialized to -(5 + (4+5+6)) = -20.

Jelly, 18 bytes

s3§§µṬṚḄ>55×S_15»0

A monadic link

Try it online!

Octave, 54 bytes

@(M)sum(prod(ismember(1:3,v=sum(reshape(M,9,5))-3))*v)

Try it online!

Port of my MATL answer.

Ruby, 78 bytes

->a{s=->x{x.flatten.sum};t=4,5,6;q=a.each_slice(3).map(&s);(t&q==t)?s[a]-15:0}

Try it online!

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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