28
\$\begingroup\$

How many petals around the rose

How many petals around the rose is a dice game you can play with your friends. Similar to "can I join the music box", there is a person that knows how the game works and the others have to discover the rule.

In this game, someone rolls some dice (usually two or more) and then people have to call "how many petals are around the rose".

Algorithm

If you want to play it by yourself, you can play it over at TIO. Just hide the header (which is where the rule is implemented) and try passing different arguments (from 1 to 6) in the function.

spoiler, what follows is the rule you are invited to find by yourself!

The "roses" here are the dice, and the "petals" are the black dots that are around a central dot. Because only the odd numbers have a central black dot, only numbers 1, 3, 5 matter for the petals. Those numbers have, respectively, 0, 2, 4 dots around the central dot, the "petals".

enter image description here

Input

Your input will be a non-empty list (or equivalent) of integers in the range [1, 6].

Output

The number of petals around the rose.

Test cases

Reference implementation in Python, that also generated the test cases.

1, 1 -> 0
1, 2 -> 0
1, 3 -> 2
1, 4 -> 0
1, 5 -> 4
1, 6 -> 0
2, 1 -> 0
2, 2 -> 0
2, 3 -> 2
2, 4 -> 0
2, 5 -> 4
2, 6 -> 0
3, 1 -> 2
3, 2 -> 2
3, 3 -> 4
3, 4 -> 2
3, 5 -> 6
3, 6 -> 2
4, 1 -> 0
4, 2 -> 0
4, 3 -> 2
4, 4 -> 0
4, 5 -> 4
4, 6 -> 0
5, 1 -> 4
5, 2 -> 4
5, 3 -> 6
5, 4 -> 4
5, 5 -> 8
5, 6 -> 4
6, 1 -> 0
6, 2 -> 0
6, 3 -> 2
6, 4 -> 0
6, 5 -> 4
6, 6 -> 0
3, 1, 5 -> 6
4, 5, 2 -> 4
4, 3, 5 -> 6
1, 4, 4 -> 0
5, 5, 2 -> 8
4, 1, 1 -> 0
3, 4, 1 -> 2
4, 3, 5 -> 6
4, 4, 5 -> 4
4, 2, 1 -> 0
3, 5, 5, 2 -> 10
6, 1, 4, 6, 3 -> 2
3, 2, 2, 1, 2, 3 -> 4
3, 6, 1, 2, 5, 2, 5 -> 10

This is so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!

\$\endgroup\$
  • \$\begingroup\$ Suggestion: Since you offer to let people play it themselves with the TIO link, consider giving a small warning of some sort before explaining what the solution/rule is. I was a bit confused after reading both paragraphs and then poking in different numbers thinking there would be some additional rule to figure out, only to find that the results were precisely as described. \$\endgroup\$ – Klaycon Mar 2 at 21:47
  • 1
    \$\begingroup\$ The intro presents the game as having some rule that must be discovered. The reader is then invited to try and find the rule themselves by visiting the TIO link, collapsing the header, and trying inputs themselves - but immediately following this invitation is an explanation of what the rule is, without any indication as such. For me at least I didn't realize that explanation was the "rule" and proceeded to try figuring it out, leading to some confusion and lost time when there seemed to be nothing more to it. \$\endgroup\$ – Klaycon Mar 2 at 22:51
  • 1
    \$\begingroup\$ @Klaycon that actually makes a lot of sense, I edited it into the challenge. I hope it makes it more clear now. \$\endgroup\$ – RGS Mar 2 at 23:04
  • 1
    \$\begingroup\$ Could you put actual spoiler tags to hide the rule? \$\endgroup\$ – JiK Mar 3 at 12:20
  • 1
    \$\begingroup\$ @Veskah you have to return 0, sorry. \$\endgroup\$ – RGS Mar 6 at 17:46

45 Answers 45

25
+200
\$\begingroup\$

Python 3, 30 bytes

lambda l:sum(n**3&6for n in l)

Try it online!

(test cases shamelessly borrowed from xnor's answer)

How?

Given \$1\le n\le6\$, the number of petals can be computed with:

$$p=n^{2k+1} \operatorname{and}6,\:k\in\mathbb{N}^*$$

where \$\operatorname{and}\$ is a bitwise operator.

This can also be written as:

$$p=2\times\left\lfloor\frac{n^{2k+1}\bmod 8}{2}\right\rfloor$$

And is based on the fact that, for any \$k\ge1\$:

$$n^{2k+1}\bmod 8=\cases{ n&\text{if $n$ is odd ($1$, $3$ or $5$)}\\ 0&\text{if $n$ is even ($2$, $4$ or $6$)} }$$

More specifically, choosing \$k=1\$:

$$p=n^3 \operatorname{and}6$$

As Python code, the resulting expression is just as long as the nice n%-2%n found by xnor. But because it ends with a digit, we can get rid of the space just before the for, saving a byte.

 n | n**3 | as binary  | AND 6
---+------+------------+-------
 1 |    1 | 00000 00 1 |   0
 2 |    8 | 00001 00 0 |   0
 3 |   27 | 00011 01 1 |   2
 4 |   64 | 01000 00 0 |   0
 5 |  125 | 01111 10 1 |   4
 6 |  216 | 11011 00 0 |   0

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ May I ask how you got to this formula? Were you playing around with the bits? \$\endgroup\$ – RGS Mar 2 at 14:01
  • 3
    \$\begingroup\$ @RGS Playing around with the bits sounds like an accurate description. Given that I wanted to find something either shorter than n%-2%n or just as long but ending with a digit, there were actually not so many (reasonable) things to try. \$\endgroup\$ – Arnauld Mar 2 at 14:12
  • 3
    \$\begingroup\$ Nice method! I posted a bounty. Amusingly, I had tried to brute-force for shorter expressions than mine, but missed yours because the intermediate step of 6**3=216 was above my cutoff. Searching now without cutoff, I don't find any other solutions except your other odd exponents as you note. \$\endgroup\$ – xnor Mar 4 at 1:37
17
\$\begingroup\$

Retina 0.8.2, 11 bytes

5
33
3
33
3

Try it online! Link includes test cases. Input can be in almost any format really as only the 5s and 3s count. Explanation:

5
33

A 5 has as many petals as two 3s.

3
33

Speaking of 3s, they have two petals, so make them count twice.

3

Count the petals.

|improve this answer|||||
\$\endgroup\$
11
\$\begingroup\$

Python, 31 bytes

lambda l:sum(n%-2%n for n in l)

Try it online!

The core expression n%-2%n evaluates to zero for even n and to n-1 for odd n.

       | n%-2   n%-2%n
-------+--------------
n even |    0        0  
n odd  |   -1      n-1
|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

R, 26 23 bytes

function(l)l%%2%*%(l-1)

Try it online!

-3 thanks to Robin Ryder!

Returns a 1x1 matrix with the result.


Old answer, since there are a handful of explicit ports:

function(l)sum((l-1)*l%%2)

Try it online!

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ 23 bytes with matrix multiplication. \$\endgroup\$ – Robin Ryder Mar 1 at 23:02
  • \$\begingroup\$ @RobinRyder thanks! I should have tried that, don't know why I didn't. \$\endgroup\$ – Giuseppe Mar 2 at 16:36
5
\$\begingroup\$

Jelly,  7  4 bytes

-3 using Giuseppe's method!

Ḃ×’S

Try it online!

How?

We know that 3's and 5's "score", that they score one less than their pips, and that \$1-1=0\$ so:

        throw   1  2  3  4  5  6
  x=throw - 1   0  1  2  3  4  5
  y=throw % 2   1  0  1  0  1  0
score = x * y   0  0  2  0  4  0

So:

Ḃ×’S - Link: throws, list of integers in [1,6]   e.g. [1,2,3,4,5,6]
Ḃ    - (throws) % 2                                   [1,0,1,0,1,0]
  ’  - (throws) - 1                                   [0,1,2,3,4,5]
 ×   - multiply                                       [0,0,2,0,4,0]
   S - sum                                            6
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Have been meaning to ask you this: where/how did you get to know the implicit things you know that some jelly atoms do? as an example, quite often you use dequeue on integers, which first implicitly converts to the range [1, .., n]. \$\endgroup\$ – RGS Mar 1 at 22:24
  • 1
    \$\begingroup\$ Being told by Dennis / Lynn / Miles or looking at the code \$\endgroup\$ – Jonathan Allan Mar 1 at 22:27
  • 1
    \$\begingroup\$ ...Dequeue is lambda z: iterable(z, make_range = True)[1:] for example. \$\endgroup\$ – Jonathan Allan Mar 1 at 22:29
5
\$\begingroup\$

Perl 6, 17 16 bytes

{sum $_>>³X+&6}

Try it online!

Uses Arnauld's formula of \$n^3\&6\$. If HyperWhatevers worked, then something like (**³+&6).sum should be possible for 13 bytes.

My old regex based solution:

{sum m:g/3|5/X-1}

Try it online!

Match 3s and 5s from the input, subtract one from each and sum them together.

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

Python, 32 bytes

lambda d:sum(r%2*~-r for r in d)

Port of Giuseppe's R answer.

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Fine - roses in dice. \$\endgroup\$ – Jonathan Allan Mar 1 at 22:48
4
\$\begingroup\$

J, 9 bytes

1#.2&|*<:

Try it online!

  • 1#. Sum of applying the following to each element...
  • 2&| Remainder when divided by 2
  • * Times...
  • <: Number decremented by 1
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

PHP, 55 53 51 bytes

fn($a)=>array_sum(array_map(fn($v)=>$v%2*~-$v,$a));

Try it online!

-2 bytes thanks to @RGS

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ You might be able to golf this considerably if you check, for example, Giuseppe's answer \$\endgroup\$ – RGS Mar 1 at 22:34
  • \$\begingroup\$ Thanks. Unfortunately my options are limited to either looping through the array or using array functions. I've plumped for the latter. The only way to apply a function to each element is to use array_map or maybe array_filter. My petal choosing lookup could be shorter, but not by much. \$\endgroup\$ – Guillermo Phillips Mar 1 at 22:43
  • \$\begingroup\$ Yeah, I only managed to get 53 bytes by using Giuseppe's formula. \$\endgroup\$ – RGS Mar 1 at 22:46
3
\$\begingroup\$

Haskell, 23 bytes

f x=sum[n-1|n<-x,odd n]

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Python 3, 40 33 32 bytes

Saved 7 bytes thanks to RGS!!!

lambda d:sum(i%2*~-i for i in d)

Try it online!

This turns out to be a total rip off of Jonathan Allan's Python answer so update him instead.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 33 bytes :) \$\endgroup\$ – RGS Mar 1 at 22:44
  • 1
    \$\begingroup\$ @RGS Nice one - thanks! \$\endgroup\$ – Noodle9 Mar 1 at 22:47
  • \$\begingroup\$ thanks, but now Jonathan Allan golfed one byte more by using clever bitwise operations... I always forget about the ~ trick. \$\endgroup\$ – RGS Mar 1 at 22:48
  • \$\begingroup\$ @RGS Was just converting the (i-1) to ~-i ... \$\endgroup\$ – Noodle9 Mar 1 at 22:51
  • \$\begingroup\$ yeah I know, I just never remember that trick by myself. \$\endgroup\$ – RGS Mar 1 at 22:51
3
\$\begingroup\$

05AB1E, 5 bytes

ÉI<*O

Try it online!

-7 byes thanks to Expired Data

As promised, someone who knows 05AB1E better than I came and golfed it somehow.

11 bytes

ε2%}Iε1-}*O

Try it online!

I'm sure someone who knows 05AB1E better than I do will come along and golf this somehow. Really simple:

ε2%}   # Map the code 2% (mod 2) to each element of the implicit input
Iε1-}  # Map the code 1- (sub 1) to each element of the explicit input
*O     # Multiply the two lists and sum
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Try it online! \$\endgroup\$ – Expired Data Mar 2 at 8:07
  • 3
    \$\begingroup\$ Everything in 05AB1E vectorizes, and É is a built-in for %2, < is a built-in for -1. \$\endgroup\$ – petStorm Mar 2 at 9:27
  • 3
    \$\begingroup\$ 4 bytes: ÉÏ<O. \$\endgroup\$ – Grimmy Mar 2 at 10:54
3
\$\begingroup\$

C# (Visual C# Interactive Compiler), 20 bytes

a=>a.Sum(x=>x--%2*x)

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Japt -mx, 3 bytes

³&6

Try it

Saved 2 bytes using Arnauld's formula so be sure to +1 him if you're +1ing this.

Original, 5 bytes

fu xÉ

Try it

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

C (gcc), 50 48 bytes

Saved 2 bytes thanks to xibu!!!

s;f(l,p)int*p;{for(s=0;l--;)s+=*p%2*~-*p++;l=s;}

Try it online!

Inputs an integer array pointer preceded by its length.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I'm assuming it's standard to allow C submissions to take the length of the array as input? +1 \$\endgroup\$ – RGS Mar 2 at 7:36
  • \$\begingroup\$ @RGS In C, only char* arrays (strings) have a sentinel value at the end. So the only way to know the size of other arrays is to be passed the length. \$\endgroup\$ – Noodle9 Mar 2 at 8:19
  • \$\begingroup\$ I thought you could do something with sizeof or something like that..? \$\endgroup\$ – RGS Mar 2 at 8:57
  • \$\begingroup\$ That's not going to work with a pointer and that's done at compile time so how's a function going to work taking different sizes of arrays? \$\endgroup\$ – Noodle9 Mar 2 at 9:01
  • \$\begingroup\$ I'm not against you taking the length as input! I just don't know much C and that is why I'm asking \$\endgroup\$ – RGS Mar 2 at 9:02
3
\$\begingroup\$

Java (JDK), 47 45 bytes

r->{int s=0;for(int i:r)s+=i%2*~-i;return s;}

First time code golfing, hope I posted this right.

-2 Bytes by removing unneeded curly brackets

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site, and good job here! \$\endgroup\$ – RGS Mar 3 at 23:00
3
\$\begingroup\$

TI-BASIC, 13 12 bytes

I found some 12 byte solutions

Ans-1:sum(Ansnot(fPart(Ans/2

The previous is the same byte count as below due to the line separator costing a byte

sum((Ans-1)2fPart(Ans/2

The above solutions use NfPart(Ans/N as a modulus operation to judge even/odd, giving us a list of dice that can have petals. Multiplying that by the original list -1 restores the petal counts to the list which can then be summed

2sum(Ans=3)+4sum(Ans=5

The original 13 byte solution simply compared the list once against 3 and separately against 5 before summing those results independently to get the petal count

All the above solutions take input as a list in Ans

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

AWK, 29 28 26 bytes

/3|5/{x+=$0-1}END{print+x}

Try it online!

-1 byte thanks to Jo King

-2 bytes thanks to user41805

+x is needed so that inputs with no odd numbers still return 0.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Taking input as lines is allowed +1 \$\endgroup\$ – RGS Mar 2 at 7:36
  • 1
    \$\begingroup\$ It appears /3|5/ works, and you should be able to remove the space after print \$\endgroup\$ – user41805 Mar 5 at 18:41
3
\$\begingroup\$

Brain-Flak, 66 60 44 42 bytes

Two bytes saved by Jo King

({(({}[()])<>)<>{<({}[()])><>([{}])<>}{}})

Try it online!

This challenge seems to ellude a straight forward solution in Brain-flak. It was quite fun to golf.

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Julia 1.0, 23 16 bytes

l->sum(l.^3 .&6)

Try it online!

-7 Bytes thanks to Maria Miller

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ 16 bytes: l->sum(l.^3 .&6) \$\endgroup\$ – Maria Miller Mar 19 at 3:27
2
\$\begingroup\$

Perl 5 -pl, 15 bytes

$\+=$_%2*$_&6}{

Try it online!

Takes the input list as one entry per line.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Roj, 134 92 88 86 65 bytes

Considerably easier than the previous challenge. Takes numbers in separate lines and end input with the input 0. Outputs via return value. (Dramatically saved 42 bytes by using dynamic input)

O=0;i=1;while i do readint i;if i==3 or i==5 do O=O+i-1 end end;O
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 8 bytesSBCS

+/⊢|¯2|⊢

Try it online!

A port of xnor's double modulo trick.

How it works

+/⊢|¯2|⊢
    ¯2|⊢  ⍝ Input modulo -2; 0 if even, -1 if odd
  ⊢|      ⍝ That modulo input; 0 if even, n-1 if odd
+/        ⍝ Sum

APL (Dyalog Unicode), 9 bytesSBCS

2∘|+.×-∘1

Try it online!

How it works

2∘|+.×-∘1
      -∘1  ⍝ Decrement input
   +.×     ⍝ Dot product with
2∘|        ⍝ Input modulo 2
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Good job using three vector dot product \$\endgroup\$ – RGS Mar 2 at 7:41
2
\$\begingroup\$

K (oK), 13 bytes

{+/(2!x)*x-1}

Try it online!

Essentially same as Jonah's J solution

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

W, 5 bytes

►S≡5u

Uncompressed:

(S2m*J

(      % Decrement input
 S     % Swap a copy of the input up
  2m   % Modulo the value by 2
    *  % Multiply them
     J % Find their sum
```
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks for your W submission. What is the d next to the language name? \$\endgroup\$ – RGS Mar 2 at 7:39
  • \$\begingroup\$ @RGS It's a flag to decompress the source; it's currently an obscure flag but is kept for backwards compatibility. (I forgot to remove the flag, whoops!) \$\endgroup\$ – petStorm Mar 2 at 8:49
2
\$\begingroup\$

Icon, 50 bytes

procedure f(a)
n:=0&n+:=!a-1=(2|4)&\z
return n
end

Try it online!

Explanation:

procedure f(a)         # the argument a is the list 
    n:=0 &             # sets the sum to 0 and  
    n+:=!a-1=(2|4) &   # add to n (n+:=) each element of a decreased by one (!a-1),
                       # if it now equals 2 or 4 (=(2|4)) and
    \z                 # loop (in fact checks if a variable z exists - since it 
                       # doesn't, backtracks to get the next element of a, if any)
    return n           # returns the sum
end
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Actually, 8 bytes

A port using Arnauld's formula.

⌠3ⁿ6&⌡MΣ

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Excel, 34 bytes

=SUMIF(A:A,3)/3*2+SUMIF(A:A,5)/5*4

SUMIF does much of the work for us.

Feel like there should be a solution using SUMPRODUCT, but have not found it yet.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Burlesque, 12 bytes

psf{2.%}?d++

Try it online!

ps     # Parse input to array
f{2.%} # Filter for mod2 == 1
?d     # Decrement each
++     # Sum
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 4 bytes

(Ç¥Σ

Try it online.

Explanation:

(     # Decrease each value in the (implicit) input-list by 1
 Ç    # Inverted filter this list by (so keep those which are falsey):
  ¥   #  Modulo-2
   Σ  # And sum each remaining item
      # (after which the entire stack joined together is output implicitly as result)
|improve this answer|||||
\$\endgroup\$

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.