26
\$\begingroup\$

It's friday! Which means it's time for beer!
Sadly though, today we will be golfing beer instead of drinking it. :(

Challenge

Output a beer and drink it. The amount of sips you take changes your output.

Sips

Your program should take one input string. This string can solely consist out of concatenated sips.
If the input is an empty string, you should output a full beer glass, including foam.

The more sips you take, the emptier your beer glass will be.

If you take 0 sips, your beer still has foam. The output of this foam is always the same (see examples).

If you take 1 sip, you should output the beer glass, followed by a new line and the string Yuck, foam..

If you take 1 or more sips, your beerglass should not contain anymore foam, but should show the top of your glass. Drinking the foam counts as one sip.

If you take 6 or more sips, you should output an empty beer glass, followed by a new line and the string Burp.

For each sip you take, your beer glass should become emptier. How full your beerglass is depends on the amount of bubbles ° (°) in your beer. For each sip you take after the foam, a line of bubbles should be removed. Each line of beer can contain a minimum of 1 and a maximum of 5 bubbles. The position of these bubbles should be 100% random.

Examples

input

empty input string, or no input at all

output

  oo  o oo 
 oooooooooooo
o|  °     ° |\
 |     °    | \
 | °°    °  |} |
 |  °    °  | /
 |     °   °|/
 \__________/

input

sip sip sip

output

 ____________
 |          |\
 |          | \
 |     °    |} |
 |° °     ° | /
 | °    °   |/
 \__________/

input

sip sip sip sip sip sip sip sip sip sip

output

 ____________
 |          |\
 |          | \
 |          |} |
 |          | /
 |          |/
 \__________/
Burp

This pastebin contains a list of inputs and outputs. Remember that the bubbles in the beerglass should be random!

Scoring

This is , shortest code in bytes wins!

Happy drinking golfing!

\$\endgroup\$
  • \$\begingroup\$ You might want to note that ° is the character with code point 176. \$\endgroup\$ – Martin Ender Nov 27 '15 at 16:30
  • 2
    \$\begingroup\$ 176 in Latin-1 and Unicode, to be more specific. \$\endgroup\$ – Mego Nov 27 '15 at 16:32
  • \$\begingroup\$ Is a trailing newline on inputs except 1 and 6+ acceptable? \$\endgroup\$ – ETHproductions Nov 27 '15 at 17:25
  • \$\begingroup\$ @ETHproductions yes. Will edit in when on laptop \$\endgroup\$ – Bassdrop Cumberwubwubwub Nov 27 '15 at 17:58
  • 1
    \$\begingroup\$ The mug outputs should remain the same. The amount of bubbles on each line should differ every run. There can be only one bubble on each line, or up to 5. That's the random part of the challenge \$\endgroup\$ – Bassdrop Cumberwubwubwub Nov 30 '15 at 7:52
9
\$\begingroup\$

Japt, 189 bytes

I almost went insane while trying to get this to work properly...

U?S+'_pC +R:"  oo  o oo\n "+'opC +"\no")+"\\, \\,} |, /,/"q', £(V=(!Y«U?"|: |" +SpA +'|+X,(1+5*Mr)o mZ=>Ul <Y*4+4©(V=Vh2+A*Mr ,'° ),V)qR +"\n \\"+'_pA +'/+R+(Ul ¥3?"Yuck, foam.":Ug22 ?`B¨p:

Try it online!

(Note: This program was made for an older version of Japt, and doesn't currently work in the latest version. To get around this, the older version is specified in the URL. Unfortunately, this also means the top-right code box doesn't work.)

This is by far the longest program I have ever written in Japt. Here's a breakdown:

Step 1: Create the top of the beer mug.

U?S+'_pC +R:"  oo  o oo\n "+'opC +"\no")

           // Implicit: U = input string
           // Begin the ASCII art with:
U?S+       //  If U is not an empty string, a space +
'_pC +R:   //   "_".repeat(12) + a newline.
:"..."+    //  Otherwise, this string +
'opC +     //   "o".repeat(12) +
"\no")     //   a newline and an "o".

If U is an empty string, this makes:

  oo  o oo
 oooooooooooo
o

Otherwise, this makes:

 ____________

Step 2: Create the middle rows of the mug.

+"\\, \\,} |, /,/"q', £(V=(!Y«U?"|: |" +SpA +'|+X,

+"..."    // Add to the previous string: this string,
q', £(    // split at commas, with each item X and its index Y mapped to:
V=(       //  Set variable V to the result of concatenating:
!Y«U?     //   If Y is 0 and U is an empty string,
"|: |"    //    "|"; otherwise, " |";
+SpA      //   10 spaces,
'|+X,     //   "|", and X.

This results in the previous string plus:

 |          |\
 |          | \
 |          |} |
 |          | /
 |          |/

Step 3: Add the bubbles.

(1+5*Mr)o mZ=>Ul <Y*4+4©(V=Vh2+A*Mr ,'° ),V)

            // Note: We're still looping through the five rows at this point.
(1+5*Mr)    // Generate a random integer between 1 and 5.
o           // Create an array of this many integers, starting at 0.
mZ=>        // Map each item Z in this range to:
Ul <Y*4+4©  //  If the length of U is less than Y*4+4,
            //  (in other words, if there's less than Y+1 "sip"s)
(V=Vh   '°  //   Insert "°" at position
2+A*Mr      //    2 + random number between 0 and 9.
),V)qR      // Finally, return V, and join the five rows with newlines.

At this point, the mug looks something like this:

 ____________
 |          |\
 |          | \
 |     °    |} |
 |° °     ° | /
 | °    °   |/

Step 4: Add the final row and optional text.

+"\n \\"+'_pA +'/+R+(Ul ¥3?"Yuck, foam.":Ug22 ?`B¨p:

+"\n \\"    // Add a newline and " \".
+'_pA       // Add 10 "_"s.
+'/+R       // Add a slash and a newline.
+(Ul ¥3?    // If the length of U is 3 (i.e. 1 "sip"),
"..."       //  add the string "Yuck, foam.".
:Ug22 ?     // Otherwise, if U has a character at position 22 (six or more "sip"s),
`B¨p        //  decompress this string ("Burp") and add it.
:           // Otherwise, add nothing.

Now everything is ready to be sent to output, which is done automatically. If you have any questions, feel free to ask!

\$\endgroup\$
  • \$\begingroup\$ It's been 3 days. RIP brain \$\endgroup\$ – Bassdrop Cumberwubwubwub Nov 30 '15 at 10:32
  • \$\begingroup\$ @Bas I had it mostly written yesterday, but it wasn't my brain so much as my browser that died ;) I'll get it written today. \$\endgroup\$ – ETHproductions Nov 30 '15 at 16:22
  • \$\begingroup\$ I'm glad your brain is still functioning, and I'm looking forward to see an explanation of this <s>monstrosity</s> beautiful piece of code \$\endgroup\$ – Bassdrop Cumberwubwubwub Nov 30 '15 at 16:32
  • 1
    \$\begingroup\$ @Bas Explanation is ready :-) \$\endgroup\$ – ETHproductions Nov 30 '15 at 18:45
4
\$\begingroup\$

JavaScript (ES6), 283 281 bytes

s=>` `+(u=`_________`,(s=s&&s.split` `.length)?u+`___
 `:` oo  o oo
 oooooooooooo
o`)+(i=0,l=q=>`|`+[...u].map(_=>Math.random()>.8&i>=s&&b++<5?`°`:` `,b=0,i++).join``+(b|i<s?` `:`°`)+`|`+q+`
 `)`\\`+l` \\`+l`} |`+l` /`+l`/`+`\\`+u+`_/
`+(s&&s<2?`Yuck, foam.`:s>5?`Burp`:``)

Explanation

s=>
  ` `+(u=`_________`,        // u = 9 underscores
  (s=s&&s.split` `.length)   // s = number of sips
    ?u+`_
 `:` oo  o oo
 oooooooooooo
o`)                          // print glass top or foam

  // Print glass lines
  +(i=0,                     // i = line number
    l=q=>                    // l = print glass line
      `|`+[...u].map(_=>     // iterate 9 times
        Math.random()>.8     // should we put a bubble here?
        &i>=s                // has this line already been sipped?
        &&b++<5              // have we already placed 5 bubbles?
          ?`°`:` `,          // if not, place the bubble!
        b=0,                 // reset the number of placed bubbles
        i++                  // increment the line counter
      ).join``               // put the 9 spaces and bubbles together
      +(b|i<s?` `:`°`)       // place a bubble at 10 if none were placed
      +`|`+q+`
 `                           // print the suffix of this glass line
  )`\\`
  +l` \\`
  +l`} |`
  +l` /`
  +l`/`

  +`\\`+u+`_/
`                            // print the bottom of the glass
  +(s&&s<2?`Yuck, foam.`
    :s>5?`Burp`:``)          // print the message

Test

Input: <input type="text" id="sips" /><button onclick="result.innerHTML=(

s=>` `+(u=`_________`,(s=s&&s.split` `.length)?u+`___
 `:` oo  o oo
 oooooooooooo
o`)+(i=0,l=q=>`|`+[...u].map(_=>Math.random()>.8&i>=s&&b++<5?`°`:` `,b=0,i++).join``+(b|i<s?` `:`°`)+`|`+q+`
 `)`\\`+l` \\`+l`} |`+l` /`+l`/`+`\\`+u+`_/
`+(s&&s<2?`Yuck, foam.`:s>5?`Burp`:``)

)(sips.value)">Go</button><pre id="result"></pre>

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

PHP, 277 265 263 bytes

Assuming 1-byte-linebreak. Add one to 14 and 17 on Windows.

$r=str_pad(($i=$argc-1)?"":"  oo  o oo",16).str_pad("
 ",14,_o[!$i])."   ".($s="
 |          |")."\\  $s \\ $s} |$s / $s/
 \__________/
 ".($i<6?$i-1?"":"Yuck, foam.":burp)if(!$i){$r[34]=o;$i=1;}for(;$i++<6;)for($n=rand(1,5);$n--;)$r[17*$i+rand(2,11)]="°";echo$r;

Run with -r. line breaks may need escaping.

breakdown

// draw beer glass
$r=
    // first line: empty or foam
    str_pad(($i=$argc-1)?"":"  oo  o oo",16)
    // second line: top or foam
    .str_pad("\n ",14,_o[!$i])
    // other lines: empty glass+bottom
    ."   ".($s="\n |          |")."\\  $s \\ $s} |$s / $s/\n \__________/\n"
    // lyrics
    .($i<6?$i-1?"":"Yuck, foam.":burp)
;

// add foam left to the glass
if(!$i){$r[34]=o;$i=1;}

// add bubbles
for(;$i++<6;)
    for($n=rand(1,5);$n--;)
        $r[17*$i+rand(2,11)]="°";

// output
echo$r;
\$\endgroup\$
  • \$\begingroup\$ Hi, sorry for failing to answer your question in time but the foam should be fixed. However, if it improves your byte count I guess it may be random too. My bad for not clearly specifying, so use it to your advantage \$\endgroup\$ – Bassdrop Cumberwubwubwub Oct 21 '16 at 8:06
  • \$\begingroup\$ @BassdropCumberwubwubwub Actually random foam costs. I just saw that everyone had it fixed while I was coding and kept mine random for a start. \$\endgroup\$ – Titus Oct 21 '16 at 8: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.