20
\$\begingroup\$

You met a 4-th dimensional being who challenged you to a game of dice. The rules are simple: each player rolls 3 6-sided dice and takes the sum of each combination of 2 dice. The player with the highest sum wins. If the first-highest sum is a tie, consider the second-highest sum, and so on.

Your opponent's dice look normal, but you think they might have more than 6 sides! Your goal is to find out if the alien rolled a number higher than 6.

Your input is a set of 3 integers, separated by at least one character (space or otherwise). Input must not be hard coded.

// All valid inputs
6 7 9
6, 7, 9
(6, 7, 9)

Your output is a string representing if the alien cheated. Any output is allowed, as long as two distinct values exist for cheating/valid. Appended whitespace is permitted.

"😀" // (U+1F600) Valid set of dice
"😠" // (U+1F620) Invalid set of dice

Examples:

<-: 8 11 9
->: 😀       // Dice can be found to be 3, 5, and 6, which are all on 6-side die
<-: 9 11 6
->: 😠       // Dice can be found to be 2, 7, and 4, which means the alien cheated!

Assumptions:

  • Input will be 3 integers, 2-12
  • Input numbers can be in any order
  • Die values will be positive integers
  • Alien only cheated if they rolled greater than 6

Write a program or function that takes inputs, returns or prints outputs.

Fewest bytes wins!

\$\endgroup\$
14
  • 1
    \$\begingroup\$ "Any stdout is allowed, as long as two distinct values exist for cheating/valid". Does this mean you can choose your own output string to stdout, as long as the output for chating/valid is distinct? For example, you can output c for cheating and v for valid? \$\endgroup\$
    – xiver77
    Mar 3 at 14:57
  • 2
    \$\begingroup\$ So (2,2,3) is an invalid input? \$\endgroup\$
    – l4m2
    Mar 3 at 14:58
  • 1
    \$\begingroup\$ @badatgolf oh thanks for the clarification. I got caught up in the format. Yes, that would be invalid, per the clause "Die values will be positive integers" \$\endgroup\$
    – Groger
    Mar 3 at 15:38
  • 2
    \$\begingroup\$ I recommend allowing that the implementation can just be a function returning a truthy/falsy value. As you can see, 2 answers are already just a function that doesn't write to stdout. That's the kind of the norm for this kind of challenges in this site. \$\endgroup\$
    – xiver77
    Mar 3 at 15:42
  • 2
    \$\begingroup\$ @badatgolf That shouldn't invalidate answers. The clause is meant to indicate that an input will always be legal. The answers aren't meant to check for validity. \$\endgroup\$
    – Groger
    Mar 3 at 15:48

21 Answers 21

13
\$\begingroup\$

Husk, 8 bytes

¬>6-▼¹½Σ

Try it online!

Outputs 1 for normal dice, 0 if the alien cheated.

Checks whether half (½) of the sum (Σ) minus (-) the smallest () element is not (¬) greater than 6 (>6).

\$\endgroup\$
0
7
\$\begingroup\$

R, 28 bytes

function(x)sum(x/2)-min(x)>6

Try it online!

Same approach as my Husk answer.

\$\endgroup\$
7
\$\begingroup\$

Haskell, 20 bytes

f l=any(<sum l/2-6)l

Try it online!

Inspired by @DominicVanEssen R answer.
Tells if it's a cheater (the alien.. not Dominic).

\$\endgroup\$
5
\$\begingroup\$

JavaScript (Node.js), 36 bytes

f=(a,b,c)=>a>b|a>c?f(b,c,a):b+c-a<13

Try it online!

Like the hypot one

\$\endgroup\$
1
  • 3
    \$\begingroup\$ "Like the hypot one" what are you referring to? \$\endgroup\$
    – Jonah
    Mar 3 at 15:35
5
\$\begingroup\$

JavaScript (ES6), 35 bytes

Expects ([a,b,c]). Returns true for cheating or false for valid.

A=>A.some(x=>eval(A.join`+`)/2>x+6)

Try it online!

\$\endgroup\$
5
\$\begingroup\$

C (gcc) (-O0), 73 bytes

#define g(a,b) if(a>b)a^=b^=a^=b;
f(a,b,c){g(a,b)g(b,c)g(a,b)a=b+c-a<13;}

Try it online!

Not the prettiest solution

C (gcc) (-O0), 38 bytes, shamelessly copy this beautiful answer.

f(a,b,c){a=a>b|a>c?f(b,c,a):b+c-a<13;}

Try it online!

-2 bytes thanks to AZTECCO

\$\endgroup\$
1
  • 2
    \$\begingroup\$ For the 40Byte answer you can do a=.. and remove s \$\endgroup\$
    – AZTECCO
    Mar 3 at 20:01
4
\$\begingroup\$

05AB1E, 7 6 bytes

O;αà6›

Inspired by @DominicVanEssen's R answer.
Takes a list [a,b,c] as input, and outputs 1/0 for cheating/valid respectively.

Try it online or verify all test cases.

Explanation:

O       # Sum the three values in the (implicit) input-list
        #  e.g. [8,11,9] → 28
        #  e.g. [9,11,6] → 26
 ;      # Halve it
        #  → 14
        #  → 13
  α     # Take the absolute difference of this sum with each value in the
        # (implicit) input-list
        #  → [6,3,5]
        #  → [4,2,7]
   à    # Pop and push its maximum
        #  → 6
        #  → 7
    6›  # Check if this is larger than 6
        #  → 0
        #  → 1
        # (after which this is output implicitly as result)
\$\endgroup\$
3
  • \$\begingroup\$ Code and byte count do not match \$\endgroup\$
    – Seggan
    Mar 3 at 16:21
  • \$\begingroup\$ Also the TIO link \$\endgroup\$
    – Seggan
    Mar 3 at 16:27
  • \$\begingroup\$ @Seggan Oops, you're completely right. I made a mistake with the 6-byter and forgot to change the byte-count and single TIO when I fixed it. Should be fixed now. \$\endgroup\$ Mar 3 at 16:27
4
\$\begingroup\$

MATL, 10 bytes

2/sGX<-IE>

Try it out / Test three cases

Port of @Dominic van Essen's answers. 1 = You cheating alien! 0 = Never had a moment of doubt.

4-sGX<E-F> Try it online! is another 10 byter.

MATL, 11 bytes

.5IXy-iY*7<

Try it out / Test three cases

Outputs truthy (all 1s) for no cheating, falsy (some 0s) for cheating.

For given input [a, b, c], the system of linear equations is : \$ d_2 + d_3 = a \$ , \$ d_1 + d_3 = b \$ , \$ d_1 + d_2 = c \$

Or in matrix form: \$ \begin{bmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 0 \end{bmatrix}\begin{bmatrix} d_1 \\ d_2 \\ d_3 \end{bmatrix} = \begin{bmatrix} a \\ b \\ c \end{bmatrix} \$

The solution - the set of dice values - is the inverse of the matrix on the left, multiplied by the input. This inverse is equal to \$ 0.5 - I_3 \$ (where \$I_3\$ is the 3x3 identity matrix).

.5IXy- computes this inverse, iY* multiplies that by the input, 7< checks if all of the resulting dice values are less than 7 (and hence valid).

\$\endgroup\$
1
  • \$\begingroup\$ I love the matrix one! I think you can use 6+G2/s<a or similar for 8 (maybe 7, though, I know there's a multiply by 2 command but I never remember what it is) porting my R answer. \$\endgroup\$
    – Giuseppe
    Mar 10 at 13:00
3
\$\begingroup\$

Retina 0.8.2, 34 bytes

\d+
$*
O`1+
^
12$*
(1+)(1+),\1,\2$

Try it online! Link includes test cases. Outputs 1 for normal dice, 0 if the alien cheated. Explanation:

\d+
$*

Convert to unary.

O`1+

Sort into order.

^
12$*

Add 12 to the smallest value.

(1+)(1+),\1,\2$

The sum should then equal or exceed the sum of the other two values.

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

Vyxal, 7 bytes

∑½?g-6≤

Try it Online!

Port of @Dominic van Essen's Husk answer. 1 if the dice are normal, 0 if the alien used tesseracts for its dice.

∑½?g-6≤ # Takes list as input
∑       # Sum of the list
 ½      # Halved..
    -   # Minus...
  ?g    # Smallest element of the input
     6≤ # Is less than or equal to 6?
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Oops, messed up my words. Thanks for noticing that. \$\endgroup\$
    – Seggan
    Mar 3 at 17:09
2
\$\begingroup\$

J, 20 11 bytes

12<+/-2*<./

-9 bytes thanks @Bubbler

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You can use a noun in the left side of a fork (so 6 works instead of 6:), you don't need abs if you reverse the direction of subtraction, you can double everything else instead of halving the sum, and you can change "max is greater than 6" 6<>./@(...) to "some number is greater than 6" 1 e.6<. These changes give 12 bytes. \$\endgroup\$
    – Bubbler
    Mar 3 at 23:38
  • \$\begingroup\$ Actually this is shorter. \$\endgroup\$
    – Bubbler
    Mar 3 at 23:43
2
\$\begingroup\$

R, 28 bytes

function(x)any(sum(x)/2>6+x)

Try it online!

Alternate and equally-golfy solution porting this python answer -- the naive port would be something like sum(x)-2*x>12 which is a byte longer.

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

Julia 1.0, 23 bytes

x->sum(x/2)-min(x...)>6

Try it online!

Port of @Dominic van Essen's answers. Output is the answer to the question "Did the alien cheat?"

Julia 1.2 or above, with using LinearAlgebra, 23 bytes

x->any((.5 .-I(3))x.>6)

(doesn't work on TIO since it only has Julia 1.0)

18 bytes without the any call, if output can be all 0s for no cheating, at least one 1 for cheating alien.

Find the dice values by solving the system of linear equations via matrices, and check if any are greater than 6. (More explanation in my MATL answer).

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

Python 3, 27 26 bytes

Code

lambda v:sum(v)/2-min(v)>6

Outputs True for cheated and False for valid.

Try it online!

Explanation

Let \$a\$, \$b\$ and \$c\$ be the three dice rolls, where \$a\$ is the highest roll. Therefore, if \$a > 6\$, the alien cheated, and otherwise they did not.

The sum of the numbers is \$2a + 2b + 2c\$. The minimum of the numbers is \$b + c\$. Therefore, sum(v)/2-min(v) is \$(2a + 2b + 2c)/2 - (b + c)\$ which is \$a\$. If this is greater than \$6\$, the alien cheated.

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

Python 3.8 (pre-release),38 28 27 bytes

Latest Answer

lambda *a:sum(a)/2-min(a)>6

Try it online!

Old Answer

lambda x,y,z:max(x-y+z,x+y-z,y+z-x)>12

Try it online!

Output : Gives True when cheated and False when not.

Thank you Aetol for the suggestion of using a+b+c-2*min(a,b,c)>12 instead of max(x-y+z,x+y-z,y+z-x)>12

This R answer gave the nice sum(a)/2-min(a)>6 Idea

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Checking a+b+c-2*min(a,b,c)>12 is shorter by 4 characters \$\endgroup\$
    – Aetol
    Mar 5 at 18:01
2
\$\begingroup\$

Pyth, 22 18 11 bytes

L>-/sb2hSb6

Port of @Lecdi's Python 3 solution.

L>-/sb2hSb6
L             # lambda b:
    sb        # sum(b)
   /  2       # sum(b) / 2
       hSb    # min(b)
  -           # sum(b)/2 - min(b)
 >        6   # sum(b)/2 - min(b) > 6
 

Try it online!

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

Charcoal, 8 bytes

›Σθ⊗⁺⁶⌊θ

Try it online! Link is to verbose version of code. Takes input as an array and outputs - if the alien cheated, nothing if it didn't. Explanation: Inspired by @DominicvanEssen's answers.

  θ         Input array
 Σ          Summed
›           Is greater than
       θ    Input array
      ⌊     Minimum
    ⁺       Plus
     ⁶      Literal integer `6`
   ⊗        Doubled
            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Ruby, 21 bytes

->*s{s.sum/2-s.min<7}

Try it online!

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

Etch, 47 bytes

s=:get;:split" ";:intify;;:out:sum s;/2-:min s;>6;

A straightforward point of this Husk answer.

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

BQN, 9 bytes

¯12>-˜´∘∨

Try it at BQN online REPL

Outputs 0 if the dice are 6-sided, 1 if the alien is cheating.

We calculate the highest die roll using a single fold (or reduce in some languages) of a "minus" operation across the 3 sums-of-2-rolls in increasing order.
Consider initial die rolls of s, m and l, where l is the (possibly non-unique) largest, and s is the smallest. Then, the sums-of-2-rolls, in increasing order, are: s+m, s+l, m+l.
Folding "minus" across this yields (s+m minus s+l) minus m+l = m-l - (m+l) = -2l. So we just need to check whether the result is less than minus 12: if it is, then l was greater than 6 and the alien was cheating.

         ∨   # sort the input
        ∘    # and use that to
     -˜´     # fold 'subtracted from' from the right
¯12>         # and check whether it's less than minus 12

This comes out 1 byte shorter than the "subtract the lowest value from half the sum of the input" approach (12<+´-2×⌊´ = 10 bytes in BQN: try it)

\$\endgroup\$
0
\$\begingroup\$
T.R>[3],[3],[3]
R.Tsqrt2=[2],[2],[2]
TTT=T
2|r1,r2|rTrsqrt2[3\34]=1.5TSQRT2
\$\endgroup\$
1
  • 14
    \$\begingroup\$ Welcome to Code Golf! What language is this? \$\endgroup\$ Mar 3 at 23:46

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.