32
\$\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\$
9
  • \$\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, 2020 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, 2020 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, 2020 at 23:04
  • 1
    \$\begingroup\$ Could you put actual spoiler tags to hide the rule? \$\endgroup\$
    – JiK
    Mar 3, 2020 at 12:20
  • 1
    \$\begingroup\$ @Veskah you have to return 0, sorry. \$\endgroup\$
    – RGS
    Mar 6, 2020 at 17:46

51 Answers 51

1
2
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)
\$\endgroup\$
2
\$\begingroup\$

Red, 48 bytes

func[b][s: 0 foreach a b[s: a - 1 *(a % 2)+ s]s]

Try it online!

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

Charcoal, 7 bytes

IΣ&⁶XA³

Try it online! Link is to verbose version of code. Port of @Arnauld's Python answer, as Charcoal supports vectorising those operations. Explanation:

     A  Input array
    X ³ Cubed (vectorises)
   ⁶    Literal `6`
  &     Bitwise And (vectorises)
 Σ      Sum
I       Cast to string for implicit print

Since is commutative, the is placed first in order to keep it separate from the ³.

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

GolfScript, 10 bytes

Port of Arnauld's Python answer.

0\{3?6&+}/

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ Aaaah, beat me to posting the 10-byter - took me a good while to notice I had to toss the 0 up front to get it to work. I spent a while getting the block to fit five bytes, and figured the cube answer just a tad too late. Well done! \$\endgroup\$
    – Mathgeek
    Mar 2, 2020 at 13:20
  • \$\begingroup\$ This appears to use hard-coded input. Is it meant to be a function, a full program or just a snippet? If it is a function then shouldn't it be reusable? \$\endgroup\$
    – Jo King
    Mar 2, 2020 at 21:55
  • \$\begingroup\$ @JoKing this takes input via one of the methods allowed by default. \$\endgroup\$
    – Grimmy
    Mar 2, 2020 at 21:59
  • \$\begingroup\$ So it's a function then? Can you reuse it? I have doubts as to whether it counts as a function when not surrounded by {} \$\endgroup\$
    – Jo King
    Mar 2, 2020 at 22:00
  • \$\begingroup\$ I don’t think it really matters whether it’s a function, since the input / output methods allowed are the same. Worst case, I can change it to inputting an array of bytes from STDIN, but that’s not gonna look pretty on TIO since byte values 1‑6 are unprintables. \$\endgroup\$
    – Grimmy
    Mar 2, 2020 at 22:07
2
\$\begingroup\$

C (gcc), 38 bytes

b;f(int*a){b=(b=*a++)?b%2*~-b+f(a):0;}

Input as 0-terminated array a.

Recursive function.

-10 bytes thanks to Arnauld!

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Using a 0-terminated list would save 3 bytes. \$\endgroup\$
    – Arnauld
    Mar 2, 2020 at 13:17
  • 2
    \$\begingroup\$ Going recursive saves 7 more bytes. \$\endgroup\$
    – Arnauld
    Mar 2, 2020 at 13:22
2
\$\begingroup\$

Ruby, 23 21 bytes

->a{a.sum{|x|x**9&6}}

Try it online!

Actually using the same idea as Arnauld, only with bigger numbers. Read his explanation and upvote him.

\$\endgroup\$
1
  • \$\begingroup\$ Might as well take a single array as input instead of splatting the 2 arguments, since some other answers are doing the same. \$\endgroup\$
    – Value Ink
    Mar 2, 2020 at 19:21
2
\$\begingroup\$

Batch, 101 90 Bytes

@Set _=0
@For %%A in (%*) Do @For %%B in (3,5) DO @IF %%A EQU %%B Set /A _+=%%B-1
@Set _

11 bytes saved using Neil's suggestions

\$\endgroup\$
5
  • \$\begingroup\$ I'm not sure that the output of SET _ counts as acceptable. But you can save some bytes by removing @ECHO OFF and putting the @ at the start of each line and after )DO @. You can also loop over 3 and 5 using a simple For loop without /L, although doing arithmetic on the value would probably be shorter still. \$\endgroup\$
    – Neil
    Mar 3, 2020 at 17:14
  • \$\begingroup\$ Correct in the savings using @ and dropping /L. old habbits die hard. As for using set to output the result, I can't say I understand why you think it wouldn't count. \$\endgroup\$
    – T3RR0R
    Mar 3, 2020 at 21:14
  • \$\begingroup\$ I have this vague memory of seeing some answer (on a different question and in a different language) where this was an issue, so I thought it was best to seek clarification. \$\endgroup\$
    – Neil
    Mar 3, 2020 at 22:31
  • \$\begingroup\$ @Neil thanks for the worries, I would say I don't really mind :) the point is clearly computing the answer \$\endgroup\$
    – RGS
    Mar 3, 2020 at 23:01
  • \$\begingroup\$ golfed another 19 bytes off: lingering environment changes (SET) causing inaccurate results are not part of the answer's concern. pastebin.com/MikFWfG9 \$\endgroup\$
    – ScriptKidd
    Apr 27, 2020 at 12:41
2
\$\begingroup\$

Bash + Core utilities, 37 35 34 30 29 bytes

sed s/5/33/\;p|tr -cd 3|wc -c

Try it online!


Shortened by 1 byte: Thanks to @user41805 for pointing out that OP is allowing the program to require one number per line, which lets me eliminate the g flag from the sed command.


Shortened by 4 bytes thanks to @Neil! The change is to use sed to print the output twice, before counting characters, eliminating the need to double the character count.


I was going to use the formula n%2*(n-1), but then I saw that everybody else had the same idea, so I decided to do something different.

Input is a list of integers between 1 and 6, inclusive, on stdin, with one number per line.

Output is on stdout.

The 30-byte version:

Uses @Neil's idea of using sed to print the line twice, eliminating the need to double the number.

The 34-byte version:

One byte shorter: instead of multiplying by 2, I now add the number to itself.

How the 35-byte version worked:

  1. Replace every instance of 5 by 33. (The number of 3s is now half the desired answer.)
  2. Delete all characters except 3. (This even eliminates the \n at the end of the line.)
  3. Use wc (without the -c option this time) to print the number of lines, the number of words, and the number of characters, separated by spaces.
  4. Use dc to double the number of characters, ignoring the number of lines and the number of words.

How the original 37-byte version worked:

  1. Replace every instance of 5 by 33.
  2. Replace every instance of 3 by 33. (In particular, every 5 in the original string has now become 3333.)
  3. Delete all characters except 3. (This even eliminates the \n at the end of the line.)
  4. Count and print the number of characters in what remains. (Every 3 in the original string has been replaced by 33, every 5 has been replaced by 3333, and everything else has been removed.)

In looking through the various submissions, it turns out that @Neil's Retina answer had used the same method as my original 37-byte version (but he was earlier). I wasn't sure at first, since I've never used Retina, but @Neil confirmed it.

The newer versions work somewhat differently, though.

\$\endgroup\$
8
  • \$\begingroup\$ Yes, my Retina answer does use the same approach; the first two pairs of lines correspond to your sed substitutions while the final line is roughly equivalent to grep -o 3|wc -l (although obviously for a single character your tr approach is golfier). \$\endgroup\$
    – Neil
    Mar 2, 2020 at 12:45
  • \$\begingroup\$ Thanks -- that's what I thought when I read your answer. Retina looks like an interesting language; I'll check it out. \$\endgroup\$ Mar 2, 2020 at 18:41
  • \$\begingroup\$ Seeing your latest answer makes me wonder - can you make sed print each line twice, thus doubling the number of 3s? \$\endgroup\$
    – Neil
    Mar 2, 2020 at 19:44
  • \$\begingroup\$ Interesting idea, @Neil -- thanks, I'll check it out. \$\endgroup\$ Mar 2, 2020 at 19:48
  • \$\begingroup\$ Thanks, @Neil! That saved 4 bytes! \$\endgroup\$ Mar 2, 2020 at 19:58
2
\$\begingroup\$

Powershell, 32 18bytes

1.."$args"|?{$_%2}

Example input 6 will output

1
3
5

Saved 14 bytes thanks to @mazzy

\$\endgroup\$
8
  • \$\begingroup\$ Hey, thanks for your submission! You may want to consider including a tio link, or at least a screenshot of the results of your program for a couple of test cases! :D Ping me when you do that! \$\endgroup\$
    – RGS
    Mar 5, 2020 at 18:15
  • \$\begingroup\$ @RGS done it. Please verify! I have run it in tio and It works. \$\endgroup\$
    – Wasif
    Mar 5, 2020 at 18:19
  • \$\begingroup\$ I don't understand your example output for input 6. I wanted you to include a link for your code. Try checking the other answers: the vast majority has a link in the end that you can click, to try their code. \$\endgroup\$
    – RGS
    Mar 5, 2020 at 18:25
  • \$\begingroup\$ I don't know how to add a tio link with code. \$\endgroup\$
    – Wasif
    Mar 5, 2020 at 18:26
  • \$\begingroup\$ It is easy. In the tio.run site, you choose the programming language you were using and put the code there. Then, on top of the page, there will be a couple of buttons. A triangle pointing to the right can be used to run the code. The button to the right shows a link. If you click that, you can copy a link to the code on tio! Then you just have to include it in your answer by substituting the link in this text: [Try it online](your link here) \$\endgroup\$
    – RGS
    Mar 5, 2020 at 22:18
2
\$\begingroup\$

PowerShell, 29 bytes

$args|?{$_%2}|%{$i+=$_-1}
+$i

Try it online!

Takes input via splatting

\$\endgroup\$
2
  • \$\begingroup\$ How can I test your submission with 3 or more inputs? :) \$\endgroup\$
    – RGS
    Mar 6, 2020 at 17:26
  • 1
    \$\begingroup\$ @RGS Like this \$\endgroup\$
    – Veskah
    Mar 6, 2020 at 17:29
2
\$\begingroup\$

Husk, 6 bytes

Σm←f%2

Try it online!

Direct port of my Haskell answer.

   f%2 Take the odd numbers
 m←    Decrement them
Σ      Sum
\$\endgroup\$
2
\$\begingroup\$

JavaScript (V8), 34 bytes

r=>r.map(i=>d+=i&1&&i>>1<<1,d=0)|d

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ i>>1<<1 -> i&~1, but since it only applies to odd numbers you can just i^1. \$\endgroup\$ Feb 4, 2023 at 5:58
2
\$\begingroup\$

Vyxal, 4 bytes

~∷‹∑

Try it Online!

~    # Filter by
 ∷   # Odd? 
  ‹  # Decrement
   ∑ # Sum
\$\endgroup\$
2
\$\begingroup\$

Jelly, 3 bytes

’ḋḂ

Try it online!

’      Decrement each number,
  Ḃ    and take each number mod 2.
 ḋ     Return the dot product of both vectors of results.
\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 16 bytes

Prompts for vector of integers

+/2 4[(3 5⍳⎕)~3]

Try it online! Courtesy of Dyalog Classic

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

Keg, -rR , 13 bytes

÷0&(¦32⑼|54⑼™

Try it online!

On days like these, kids like you, should be grateful that SBCS's exist. Because otherwise, this'd be 21 bytes.

Input is taken as a number with no commas. So a roll of 5, 5, 3 would be inputted as 553.

Explanation

÷           # Item split the implicit input.
0&          # Store 0 in the register.
(           # For each item in the stack:
    ¦       #   Switch statement:
    32⑼     #   If the item is 3, add 2 to the register.
    |54⑼    #   If the item is 5, add 4 to the register.
    ™       #   End switch statement.

# -rR prints the value in the register rawly. In other words, as a number.
\$\endgroup\$
3
  • \$\begingroup\$ What are the rR flags for? \$\endgroup\$
    – RGS
    Mar 2, 2020 at 8:58
  • \$\begingroup\$ @RGS -rR prints the value in the register rawly. In other words, as a number. \$\endgroup\$
    – lyxal
    Mar 2, 2020 at 9:04
  • 1
    \$\begingroup\$ @RGS more accurately: at the end of the program, whatever is stored in the register will be printed as if ⑻. had been appended to the program after autocompletion of brackets. \$\endgroup\$
    – lyxal
    Mar 2, 2020 at 10:06
1
\$\begingroup\$

Rabbit~, 31

_[.0{.51}{+.2}{.53}{+.4}>]_+++:

Explanation:

_                               - Move to input
 [.0                    >]      - Loop over die rolls
    {.51}{+.2}                  - If die roll is 3 add 2
              {.53}{+.4}        - If die roll is 5 add 4
                          _+++: - Move out of the way, flush and print addition result

51 and 53 is ascii for 3 and 5

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

Pure data, 401 bytes

#N canvas;#X obj 0 0 inlet;#X obj 0 0 list split 1;#X obj 0 0 i;#X obj 0 0 outlet;#X msg 0 0 0;#X obj 0 0 t l;#X obj 0 0 del;#X obj 0 0 expr $i2+$i1%2*($i1>>1);#X obj 0 0 i;#X connect 0 0 4 0;#X connect 0 0 5 0;#X connect 1 0 7 0;#X connect 1 1 5 0;#X connect 1 2 6 0;#X connect 2 0 3 0;#X connect 4 0 8 0;#X connect 5 0 1 0;#X connect 6 0 2 0;#X connect 7 0 8 0;#X connect 8 0 7 1;#X connect 8 0 2 1;

Pure Data is a graphical programming language for audio processing.

Here is what my the source looks like:

enter image description here

It is pretty unreadable since I saved bytes by placing all elements at coordinate 0 0. Here it is reorganized so you can read the elements.

enter image description here

It is important to note that execution in pure data sometimes depends on the order in which objects were created, which is not visible in images like this. For this answer to work The inlet object must be connected to the 0 message before the t 1 object. This shows up in the source code as that connection being earlier on.

Most of the program is just list processing. The logic is actually handled in the expr block

#X obj 0 0 expr $i2+$i1%2*($i1>>1);

Here $i1 is the current element and $i2 is the result of the last expr. So this bitshifts the element down by one, which is intended result for odd numbers. To make even numbers we then multiply it by its 2 modulus, which is zero for even numbers and 1 for odd numbers.


Thanks to 0 ' for working on this with me.

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

Thunno, \$ 7\log_{256}(96)\approx \$ 5.76 bytes

g2%k1-S

Attempt This Online!

Port of Kevin Cruijssen's MathGolf answer.

Thunno, \$ 9\log_{256}(96)\approx \$ 7.41 bytes

D2%s1-z*S

Attempt This Online!

Port of Jonathan Allan's Jelly answer.

Explanations

g2%k1-S  # Implicit input
g  k     # Filter for those which are:
 2%      #  Odd
    1-   # Subtract one from each
      S  # Sum

D2%s1-z*S  # Implicit input
D2%        # Duplicate and mod by 2
   s1-     # Swap and subtract one
      z*   # Multiply
        S  # Sum
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 S, 4 bytes

ɗ$⁻×

Try it online!

Explanation

ɗ$⁻×  # Implicit input
ɗ     # Mod each item by 2
 $    # Push the input again
  ⁻   # Decrement each item
   ×  # Multiply elementwise
      # Implicit output
\$\endgroup\$
0
\$\begingroup\$

JavaScript (V8), 29 bytes

r=>r.map(i=>d+=i%2*--i,d=0)|d

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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