The weird and wild bean game

11

Maybe some of you already know this game: you have a collection of jelly beans of different colors. For every color, the bean can have different tastes, some are good and some are bad, and you cannot tell them apart. You have to pick a bean of a given color, and pray you have select a good one.

So, write the shortest program that receives a color of your choice (from a given list), and randomly returns the taste selected. The taste has to be picked from a built-in list. The possible list of inputs and outputs are:

Input      Output choices [only one from the list]
--------------------------------------------------
green      lawn clippings, lime, mucus, pear
yellow     rotten eggs, buttered popcorn
blue       toothpaste, blue berry
orange     vomit, peach
brown      canned dog food, chocolate
white      stinky socks, tutti-frutti, baby diapers, coconut

Rules:

  • You can assume that the input is going to be always a color from the input choices.
  • Case and trailing spaces and/or newlines do not matter.
  • Output must be uniformly random: succesive program executions must yield different results, and the chances of getting a given taste must be the same for all tastes in a list.

This is , so may the shortest program win!

Charlie

Posted 2017-06-09T11:49:32.230

Reputation: 11 448

In one part of your question, you state that the taste will be selected from a given list, implying that we will receive it as input. However, in your examples, it seems the opposite is so. – Okx – 2017-06-09T11:53:35.503

@Okx sorry, is it better now? I'm still getting used to posting here... – Charlie – 2017-06-09T11:56:57.023

1Maybe state that the output must be uniformly random so that all possible outputs have the same odds of appearing or else I can just add 2 options from each list. – LiefdeWen – 2017-06-09T11:57:17.953

@StefanDelport thank you, fixed! – Charlie – 2017-06-09T11:58:55.367

1It's a bit late now, but it should probably have been blueberry not blue berry. – Jonathan Allan – 2017-06-09T20:39:06.147

Answers

7

C#, 418 313 305 271 bytes

s=>{var a="lawn clippings,lime,mucus,pear|rotten eggs,buttered popcorn|toothpaste,blue berry|vomit,peach|canned dog food,chocolate|stinky socks,tutti-frutti,baby diapers,coconut".Split('|')["eluaoi".IndexOf(s[2])].Split(',');return a[new System.Random().Next(a.Length)];}

Too long even for C# but I can't see how to get it any shorter.

Full/Formatted version:

using System;

class P
{
    static void Main()
    {
        Func<string, string> f = s =>
        {
            var a = "lawn clippings,lime,mucus,pear|rotten eggs,buttered popcorn|toothpaste,blue berry|vomit,peach|canned dog food,chocolate|stinky socks,tutti-frutti,baby diapers,coconut"
                    .Split('|')["eluaoi".IndexOf(s[2])].Split(',');

            return a[new System.Random().Next(a.Length)];
        };

        Console.WriteLine(f("green"));
        Console.WriteLine(f("yellow"));
        Console.WriteLine(f("blue"));
        Console.WriteLine(f("orange"));
        Console.WriteLine(f("brown"));
        Console.WriteLine(f("white"));

        Console.ReadLine();
    }
}

TheLethalCoder

Posted 2017-06-09T11:49:32.230

Reputation: 6 930

That's a hell of a golf! +1 – Shaggy – 2017-06-09T13:17:41.107

@Shaggy Thanks :) I started off doing it a really naive way and slowly realised the shorter way of doing things. I had it stuck in my head I'd need a dictionary and then saw your answer using the string and splitting and realised the path to the light! – TheLethalCoder – 2017-06-09T13:19:39.243

5

JavaScript (ES6), 235 bytes

I need to figure out how to compress strings in JS!

c=>(a="lawn clippings,lime,mucus,pear|rotten eggs,buttered popcorn|toothpaste,blue berry|vomit,peach|canned dog food,chocolate|stinky socks,tutti-frutti,baby diapers,coconut".split`|`["eluaoi".search(c[2])].split`,`)[new Date%a.length]

If that's not "random enough" for your tastes then add 7 bytes replacing new Date with Math.random().

c=>(a="lawn clippings,lime,mucus,pear|rotten eggs,buttered popcorn|toothpaste,blue berry|vomit,peach|canned dog food,chocolate|stinky socks,tutti-frutti,baby diapers,coconut".split`|`["eluaoi".search(c[2])].split`,`)[Math.random()*a.length|0]

Try it

f=
c=>(a="lawn clippings,lime,mucus,pear|rotten eggs,buttered popcorn|toothpaste,blue berry|vomit,peach|canned dog food,chocolate|stinky socks,tutti-frutti,baby diapers,coconut".split`|`["eluaoi".search(c[2])].split`,`)[Math.random()*a.length|0]

r=(d=document).createElement("input");r.name="i";r.type="radio";l=d.createElement("label");j="Kiwi,sour_lemon,berryBlue,OrangeSherbet,rootBeer,Coconut".split`,`;for(s in e="green,yellow,blue,orange,brown,white".split`,`){r=r.cloneNode();l=l.cloneNode();l.setAttribute("for",r.id=r.value=e[s]);l.style.backgroundImage=`url(https://cdn-tp1.mozu.com/9046-11441/cms//files/${j[s]}.jpg)`;g.prepend(r,l);}onchange=_=>o.innerText=(v=(i=d.querySelector(":checked")).value)+": "+f(v,i.checked=0)
body{align-items:center;background:#eee;display:flex;flex-wrap:wrap;height:100vh;justify-content:center;margin:0;text-align:center;}#g{background:#fff;box-shadow:5px 5px 5px #ccc;padding:10px;}input{display:none;}label{background-repeat:no-repeat;background-size:contain;cursor:pointer;display:inline-block;height:64px;margin:10px;width:75px;}#o{font-family:monospace;font-size:18px;margin:10px auto;text-align:center;width:100%;}
<div id=g><pre id=o>click a jelly bean</pre></div>

Shaggy

Posted 2017-06-09T11:49:32.230

Reputation: 24 623

3Nice idea with eluaoi, I thought of it by myself and thought, "Oooh look how clever I'm being" then saw you beat me to it! – TheLethalCoder – 2017-06-09T12:34:26.180

new Date%a.length isn't "uniformly random". – Olivier Grégoire – 2017-06-09T12:43:31.973

Thanks, @TheLethalCoder - I was that lazy, I nearly didn't bother checking for uniqueness past the 2nd characters! – Shaggy – 2017-06-09T13:21:02.870

3Heh, I think about 4 or 5 of us came up with eluaoi around the same time :P – ETHproductions – 2017-06-09T13:21:52.603

@OlivierGrégoire, this solution predates that requirement but I've added another option that uses Math.random instead. – Shaggy – 2017-06-09T13:22:03.157

5

05AB1E, 126 bytes

¨¤.•4Õ!Õ•.•QCQΓ^ïTÁÕ#HвΘÊÓΘñ…~çÌùY$J*shÉÉk‹Ú&žвZÍζö<^'¢βŽÚq¡eζd`Ãó¨₅γ!i"v_Ym¤ÓδVË5¥vżQЉøΣγ9∞\L‰,ǝ¦8VÜUт:x+sм•'x'-:'q¡'j¡€.R‡

Explanation:

¨¤                              Get the second to last character of the string
  .•4Õ!Õ•                       Compressed string: "eougwt"
         .• .. •                Compressed base-27 string
                'x'-:           Replace "x" with "-" (for tutti-frutti)
                     'q¡        Split on 'q'
                        'j¡     Split each on 'j'
                           €    For each...
                             .R  Select a random element
                               ‡ Transliterate

If anyone's wondering, here's the uncompressed string:

lawn clippingsjlimejmucusjpearqrotten eggsjbuttered popcornqtoothpastejblue berryqvomitjpeachqcanned dog foodjchocolateqstinky socksjtuttixfruttijbaby diapersjcoconut

I can probably compress it more using some clever tricks and the dictionary, though.

Try it online!

Okx

Posted 2017-06-09T11:49:32.230

Reputation: 15 025

You can compress "eougwt" to .•4Õ!Õ• for -1. – Erik the Outgolfer – 2017-06-09T13:14:46.327

@EriktheOutgolfer When I tried that, I was comparing the length of eougwt and .•4Õ!Õ•, instead of "eougwt" and .•4Õ!Õ•. Oops. – Okx – 2017-06-09T13:18:32.157

4

Jelly, 101 100 bytes

3ị“©ȷ#Ȧ-»iị“'æLṬẏeṃɼẹ-N0ṁH)A“¬ɗ-ṃȥḞ“I$aṇṾjð4“£Ʋṛ÷pḶƥƑL]p“÷Hnøgİỵ£@ḥEḶƑƤi÷Ḃ\oŻẆ#ụqU½b“ḥĠḄĿĖṇ⁻Œḳ-¬"»ỴX

Try it online!

Erik the Outgolfer

Posted 2017-06-09T11:49:32.230

Reputation: 38 134

3

Python 2, 301 258 bytes

lambda x:choice({'e':'lawn clippings,lime,mucus,pear','l':'rotten eggs,buttered popcorn','u':'toothpaste,blue berry','a':'vomit,peach','o':'canned dog food,chocolate','i':'stinky socks,tutti-frutti,baby diapers,coconut'}[x[2]].split(','))
from random import*

Try it online!

Saved very many bytes by shortening the keys to use the 2nd index of the input, as suggested by @TheLethalCoder, and by splitting on commas instead of using a direct list.

Comrade SparklePony

Posted 2017-06-09T11:49:32.230

Reputation: 5 784

1Use eluaoi as the dictionary keys and access it with index 2 of the string should save you bytes. – TheLethalCoder – 2017-06-09T12:32:03.177

Big (y) to LethalCoder's idea – officialaimm – 2017-06-12T08:12:04.823

3

Japt, 148 146 bytes

`È}hpŠ1½ue ¼rry
lawn c¦ppÄ1Ҙ1muc«1pe‡
vÇ1pea®
ÐXky socks1ÉÍi-frÔk1baby ¹ap€s1¬¬n©
¯nšd ºg food1®o¬ÓŠ
݁ eggs1瘪 pop¬rn`·g`uÁ4`âUg2¹q1 ö

Try it online!

Saved 6 bytes thanks to Shaggy and ETHproductions

Tom

Posted 2017-06-09T11:49:32.230

Reputation: 3 078

Split on R instead of 0 to save a byte. Also, you might be able to compress eluaoi by playing around with the order. – Shaggy – 2017-06-09T12:19:11.670

@Shaggy How would that save a byte? I would need to call qR which is the same length? – Tom – 2017-06-09T12:22:30.610

Check the Unicode​ shortcuts in the docs ;) – Shaggy – 2017-06-09T12:25:44.150

Sorry, forgot to say you can remove the space before ® too. – Shaggy – 2017-06-09T12:30:45.180

Nice, almost exactly what I had. You only need 5 of the 6 chars in the string because the missing one will have an index of -1, and therefore gets the last item in the array. If you use eaiou as the string you can compress it down to three bytes (there may be other three-byte combinations). – ETHproductions – 2017-06-09T12:58:29.360

Also, if you do q1 after the correct line has been corrected you can save a byte that way. – ETHproductions – 2017-06-09T13:03:15.113

142 bytes, which I think is pretty much the same as the improvements @ETHproductions suggested. – Shaggy – 2017-06-09T13:39:47.490

3

Jelly,  95  94 bytes

OḄị“÷Hnøgİỵ£@ḥEḶƑƤi÷Ḃ\oŻẆ#ụqU½b““¬ɗ-ṃȥḞ“'æLṬẏeṃɼẹ-N0ṁH)A“ḥĠḄĿĖṇ⁻Œḳ-¬"““I$aṇṾjð4“£Ʋṛ÷pḶƥƑL]p»ỴX

A monadic link accepting a list of (lowercase) characters and returning a list of characters.

Try it online! or eat a packet of 48.

How?

Eighty-nine of the ninety-four bytes are a compressed list of eight strings. Two of these are empty strings and the other six are each newline separated flavours for one of the colours:

“...““...“...“...““...“...»
“...““...“...“...““...“...»
“                         » - a compression of dictionary words & or strings
    ““   “   “   ““   “     - list separations
    ^            ^          - the two empty lists of characters
 ...^ ... ... ...^ ... ...  - bytes used to encode the data
 wht  org grn ylw^ blu brn  - with the colours indicated. For example:
  1 2  3   4   5 6  7   0   -   “ḥĠḄĿĖṇ⁻Œḳ-¬"» is a compression of:
                            -     word     + word   + string + word       + word
                             -     "rotten" + " egg" + "s\n"  + "buttered" + " popcorn"
                             - and is at the fifth index, relating to "yellow"

The rest of the program parses the input to decide which list to use, splits the chosen list by newlines and picks a random element:

OḄị“...»ỴX - Main link:s e.g.: "blue"           "yellow"                  "brown"              "white"               "orange"                 "green"
O          - cast to ordinals  [98,108,117,101] [121,101,108,108,111,119] [98,114,111,119,110] [119,104,105,116,101] [111,114,97,110,103,101] [103,114,101,101,110]
 Ḅ         - from binary       3276             7125                      1151                 6899                  3272                     3489
  ị        - index into - 1 based and modular with 8 items so...
           -          indexes: 3276%8=4         7125%8=5                  1151%8=7             6899%8=3              3272%8=0                 3489%8=1
        Ỵ  - split at newlines (gets the list of flavours for the chosen colour)
         X - random choice (gets one of those flavours at random)

Jonathan Allan

Posted 2017-06-09T11:49:32.230

Reputation: 67 804

2

Java, 288 bytes

s->{String[]a="lawn clippings,lime,mucus,pear#rotten eggs,buttered popcorn#toothpaste,blue berry#vomit,peach#canned dog food,chocolate#stinky socks,tutti-frutti,baby diapers,coconut".split("#")["eluaoi".indexOf(s.charAt(2))].split(",");return a[new java.util.Random().nextInt(a.length)];}

Test it yourself!

Could be golfed by using a char[].

However the random part can't be "uniformly distributed" without the explicit use of Random.nextInt(int). Even (int)(Math.random()*a.length) isn't uniformly distributed.

Olivier Grégoire

Posted 2017-06-09T11:49:32.230

Reputation: 10 647

Hmm, I din't get the explanation about why you have to use new java.util.Random().nextInt(a.length) instead of (int)(Math.random()*a.length).. – Kevin Cruijssen – 2017-06-13T12:33:19.670

1Math.random() provides a number that has some specificities (a power of 0 and a sign of 0 and 52 random bits). So you actually use an entropy of 52 without any further check. If length is 3, for instance, 2^52 is not divisible by 3. So it is not randomly distributed. This is why Random.nextInt(int) (actual java file, not javadoc, at line 394) has a loop mechanism to make sure the number is amongst fair numbers. Unless I'm said "it's good enough", only Random.nextInt(n) is fair. – Olivier Grégoire – 2017-06-13T13:06:51.400

@KevinCruijssen My mistake: it's 53 random bits, not 52. – Olivier Grégoire – 2017-06-13T13:16:15.150

1Ah ok, thanks for the explanation. So Math.random() can't be used when 2^53 cannot be evenly divided by the number you multiply it with? So if you want a random number from 0-3, (int)(Math.random()*4) is acceptable to evenly divide it (four time 2251799813685248), but when you use *3 instead it isn't (which is three times 3002399751580330.666...), because casting it to int floors it, making some parts 1 smaller than others. And since length is variable in your case, it's not uniformly distributed either (with the possibility of being length 3). – Kevin Cruijssen – 2017-06-13T13:27:06.160

1Yep, you understood it all! If someone says "random", use Math.random(), if someone says something along "uniformly" or "fairly" random, use java.util.Random. That's also why I complained about Shaggy's answer. – Olivier Grégoire – 2017-06-13T13:35:45.387

1

T-SQL, 432 423 375 367 336 295 bytes

Finally, a set-based operation!!

SELECT TOP 1 SUBSTRING(value,2,99)
FROM t,STRING_SPLIT('elawn clippings-elime-emucus-epear-lrotten eggs-lbuttered popcorn-utoothpaste-ublue berry-avomit-apeach-ocanned dog food-ochocolate-istinky socks-itutti-frutti-ibaby diapers-icoconut','-')
WHERE SUBSTRING(c,3,1)=LEFT(value,1)
ORDER BY NEWID()

(Line breaks are for display and are not counted in total.)

Input is via column c in named table t, per our guidelines.

I'm simply joining our input table to a table full of the valid color/flavor combinations, then selecting a random row. ORDER BY NEWID() is a common way to randomize sort order in SQL. Depending on how strict you are, you might not consider it perfectly uniformly random, but it should be sufficiently random for jelly bean selection.

EDIT 1: Saved 9 bytes by using only the 3rd character of the color, inspired by other answers.

EDIT 2: Saved 48 bytes by putting the color flag and flavor in a single column. Lots of characters saved in the INSERT.

EDIT 3: Saved 8 bytes by replacing INSERT INTO b(o) with INSERT b

EDIT 4: Saved 31 more bytes by joining straight to the virtual table of VALUES and therefore eliminating the CREATE TABLE and INSERT.

EDIT 5: Save 41 bytes by upgrading to SQL 2016-only STRING_SPLIT function, which allows me to eliminate the variable and dynamic SQL execution.

BradC

Posted 2017-06-09T11:49:32.230

Reputation: 6 099

1

><>, 311 bytes

</"y"/\_  _
v\i-?\x"sgge nettor"
v/"l"/\"nrocpop derettub"
v\i-?\x"etsaphtoot"
v/"o"/\"yrreb eulb"
v\i-?\x"etalocohc"
v/"n"/\"doof god dennac"
v\i-?\x"timov"
v/"n"/\"hcaep"
v\i-?\>x\/~~"srepaid ybab"
v"lime" x"sgnippilc nwal"
v"pear"x  _"sucum"
v    \~__>x\
v"coconut" x"skcos yknits"
>l?!;/\  \x_"itturf-ittut"

Try it online, or watch it at the fish playground

Featuring SK cosy knits and Doof God Dennac!

Explanation: The fish's first task is to figure out what the input word is by zig-zagging down the left hand side. The fish can only read one letter at a time, and it takes fewer bytes to do this destructively. First, the fish reads the first letter and asks if it's "y" — if it is, the word is "yellow", otherwise it moves on. It then reads the second letter — if it's "l", the word is "blue", otherwise it moves on; and so on. If it reads five letters and they don't match "y" (Yellow), "l" (bLue), "o" (brOwn), "n" (oraNge) or "n" (greeN) respectively, then the colour must have been "white".

Next comes the random bit. For the colours with two possible outputs, this is pretty straightforward — for yellow, for example, the fish enters the following code at the x:

/\
\x"sgge nettor"
 \"nrocpop derettub"

The x sets the direction randomly: if it's up or left, the fish swirls around the mirrors back to the x, but if it's right or down, it reads "rotten eggs" or "buttered popcorn" (in reverse).

The four-way splits (for white and green) are messier, but they follow the same general principal — the first is:

     >x\
"lime" x"sgnippilc nwal"
"pear"x   "sucum"
      __

Note that if the fish swims up from the first x, it passes through eight "s, which toggle string mode on and off four times, then it hits a mirror and swims back down.

To get to the last four-way split, the fish has to swim through the e and r of "lime" and "pear", which add e = 14 to the stack (and reverse it), so we first have to delete that with ~. One of the four branches also involves swimming through a junk string, "> ", which we delete with ~~.

    \   /~~"srepaid ybab"
    e   "
    r    _
    \~  >x\
"coconut" x"skcos yknits"
        \x_"itturf-ittut"
         _

Finally, after adding one of the bean flavours to the stack, the fish reaches the stream of vs in the leftmost column, which sends it to

v    \
v    o
>l?!;/

which prints characters (using one of the os in "coconut") until there are none left.

Not a tree

Posted 2017-06-09T11:49:32.230

Reputation: 3 106

0

PHP, 242 bytes

<?=($a=explode(_,[e=>'lawn clippings_lime_mucus_pear',l=>'rotten eggs_buttered popcorn',u=>'toothpaste_blue berry',a=>vomit_peach,o=>'canned dog food_chocolate',i=>'stinky socks_tutti-frutti_baby diapers_coconut'][$argn[2]]))[array_rand($a)];

Try it online!

Jörg Hülsermann

Posted 2017-06-09T11:49:32.230

Reputation: 13 026

0

Mathematica, 247 bytes

R=RandomChoice
green=R@{lawn clippings,lime,mucus,pear}
yellow=R@{rotten eggs,buttered popcorn}
blue=R@{toothpaste,"blue berry"}
orange=R@{vomit,peach}
brown=R@{canned dog food,chocolate}
white=R@{stinky socks,tutti-frutti,baby diapers,coconut}
#&

Input form

[green]

J42161217

Posted 2017-06-09T11:49:32.230

Reputation: 15 931

Can you index into the string and use the eluaoi trick? I don't know mathematica so just an idea. – TheLethalCoder – 2017-06-09T14:40:01.517

0

Clojure, 231 bytes

#(rand-nth({\e["lawn clippings""lime""mucus""pear"]\l["rotten eggs""buttered popcorn"]\u["toothpaste""blue berry"]\a["vomit""peach"]\o["canned dog food""chocolate"]\i["stinky socks""tutti-frutti""baby diapers""coconut"]}(get % 2)))

Same idea as the others, I can just save some space compared to other languages. Compressing strings seems like a lost cause.

MattPutnam

Posted 2017-06-09T11:49:32.230

Reputation: 521