Code me a cookie

16

2

Challenge

Code an ASCII cookie of a kind according to the input.

Input

  • The cookie kind. This is one of the three strings: "Plain", "Chocolate", or "Nuts". May be function arguments, stdin (or closest equivalent), or file arguments.

Output

  • An ASCII cookie. See below.

Must be

     ___
    /   \
    |   |
    \___/

for Plain input,

     ___
    /. .\
    | . |
    \___/

for Chocolate input, and finally

     ___
    /^  \
    |^ ^|
    \___/

for Nut input.

Other information

  • This is my first challenge, it's as simple as can be. Constructive feedback is greatly appreciated.
  • Use any means to do so.
  • Trailing spaces are fine.
  • This is a code golf challenge, so shortest entry at the end of 9 days (Wednesday, the 12th of August 2015) wins.

Thank you!

The winner is Jakube using Pyth with 41 bytes. Thank you to all who participated. I will now task myself with coming up with more complicated challenges.

The_Basset_Hound

Posted 2015-08-03T22:58:22.033

Reputation: 1 566

7

This is nice, but rather simple as it is. It could be massively improved by requiring user input for the diameter or quantity of cookies of each type. Do the chocolate chips and and nuts have to be in the location per the examples? (with variable diameter, they could perhaps be random.) That said, it's bad form to change the challenge after posting. I suggest you either 1. leave it as it is, or 2. delete it, think a bit more about it and/or post it in http://meta.codegolf.stackexchange.com/q/2140/15599 then repost later.

– Level River St – 2015-08-03T23:07:18.113

@steveverrill Alright, thank you for the feedback. – The_Basset_Hound – 2015-08-03T23:14:32.367

1Allowing multiple toppings could have been another way to make this more interesting. Then again, simple looking challenges often get a lot of participation. BTW, two of the cookies have leading white space in the sample output. That's probably not intentional? – Reto Koradi – 2015-08-03T23:14:39.960

1I made a minor edit to cancel the indent, so all the cookies have the same leading whitespace. I assume the four leading spaces are a formatting issue and are not required? You should specify whether leading/trailing newlines are allowed. I would suggest disallowing unnecessary whitespace, with the exception of allowing an optional trailing newline. – Level River St – 2015-08-03T23:21:09.957

1Will functions be permitted? – MayorMonty – 2015-08-04T01:39:38.267

@SpeedyNinja Yes, "Use any means to do so." – The_Basset_Hound – 2015-08-04T01:56:00.740

1

After @steveverrill opened my eyes, this seems to be just a simplified version of Do you want to code a snowman?. Some further requirements, like the mentioned variable diameter, would really improve it.

– manatwork – 2015-08-04T10:36:38.093

Can the chips/nuts be positioned anywhere inside the cookie? Or only in the positions you put in the example – Beta Decay – 2015-08-04T10:40:10.743

@BetaDecay Sorry for a late reply, the chips and nuts should be in the same positions they are in the examples. – The_Basset_Hound – 2015-08-04T21:22:33.513

@BassetHound Ah okay, thanks :) – Beta Decay – 2015-08-04T21:30:46.057

Answers

4

Pyth, 42 41 bytes

X" ___
/d a\\
|cac|
\___/"G.>"^X .  .^"Cz

Try it online: Regular Input / Test Suite

Explanation:

 "..."                      template string
X     G                     replace "a..z" in ^ with:
                   Cz         convert z to an integer (base 256 of ord(char))
       .>"^X .  .^"           rotate "^X .  .^"
                              ["Plain"     -> " .  .^^X", 
                               "Chocolate" -> ".  .^^X ", 
                               "Nuts"      -> " .^^X . "]

Jakube

Posted 2015-08-03T22:58:22.033

Reputation: 21 462

7

Ruby, 73

->s{' ___
/'+['^  \
|^ ^','. .\
| . ','   \
|   '][s[0].ord%3]+'|
\___/'}

This is an anonymous lambda function. Here it is in a test program:

g=->s{' ___
/'+['^  \
|^ ^','. .\
| . ','   \
|   '][s[0].ord%3]+'|
\___/'}

puts g.call(gets)

It just uses the first letter of the cookie type (in uppercase) and takes it modulo 3 to get an index in the range 0..2. Then it returns the string representing the cookie, with the appropriate strings embedded in the right places.

Level River St

Posted 2015-08-03T22:58:22.033

Reputation: 22 049

What do you think the ord method can do if you call it for a whole string? My first idea was formatting: ->s{" ___\n/%1$s \\\n|%1$s %1$s|\n\\___/"%'^. '[s.ord%3]} – manatwork – 2015-08-04T06:32:46.910

Forget it. Once again tr proves to be shorter: ->s{' ___↵/% \↵|% %|↵\___/'.tr ?%,'^. '[s.ord%3]} – manatwork – 2015-08-04T06:39:02.007

@manatwork thank you for your suggestions. I missed s[0] --> s, it never occurred to me to try it. Your code doesn't seem to give the right answer for the chocolate case, as the chocolate chips are in different places than the nuts. Nevertheless there's some useful ideas there, I will look at them later. I haven't used tr or % before. – Level River St – 2015-08-04T10:21:42.680

Oops. You are right. That was a quick try BC (before coffee). Too early to notice the chips placement difference. :( (BTW, the “%” has nothing to do with tr's syntax. Is just a character not involved in the cookie art that I used as placeholder.) – manatwork – 2015-08-04T10:28:23.710

4

Python 2.7.6, 99 bytes

def c(t):n=hash(t)%3;return" ___\n/"+" ^."[n]+" "+"  ."[n]+"\\\n|"+[" ","^ ^"," . "][n]+"|\n\\___/"

This algorithm relies on the fact that hash(cookie)%3 gives 0 when cookie = "Plain", 1 when cookie = "Nut and 2 when cookie = "Chocolate. If anyone knows a way to make this code shorter, please let me know in the comments.

Loovjo

Posted 2015-08-03T22:58:22.033

Reputation: 7 357

Optional arguments are most certainly allowed – Beta Decay – 2015-08-04T19:42:53.947

"Use any means to do so." Yes, optional arguments are allowed. – The_Basset_Hound – 2015-08-04T19:44:07.800

Okay, but I think I'll still stick with the first one and leave the second one as it is. – Loovjo – 2015-08-04T19:46:48.007

@BetaDecay I just don't think that optional arguments really are in the spirit of code-golf. I don't really know why, I just think that it shouldn't be allowed. Now that they are both the same length, I removed the optional-arguments version. – Loovjo – 2015-08-04T19:58:14.473

3@Loovjo Using strange and unusual methods are the whole spirit of code golf :) – Beta Decay – 2015-08-04T20:01:23.037

@BetaDecay Okay, but I think I'll still use the original version. – Loovjo – 2015-08-04T20:01:55.107

You can join "\\\n"+"|" into "\\\n|". Doing so will bring it down around 9 chars. – Kamehameha – 2015-08-04T20:13:53.700

You can condense the " ^ "[n]+" ."[n]+" ^ "[n] part into a single [" ","^ ^"," . "][n]. That kills around 4 bytes more :) – Kamehameha – 2015-08-04T20:25:25.130

Which Python version is this? hash behaviour changes between versions – Sp3000 – 2015-08-04T22:57:02.367

@Sp3000 2.7.6. I didn't know that. Silly me. – Loovjo – 2015-08-05T05:52:12.883

3

C: 122

q(char *p){char *t,*m;int i=*p%3;t=i?i%2?". .":"   ":"^  ";m=i?i%2?" . ":"   ":"^ ^";printf(" ___\n/%s\\ \n|%s|\n\\___/",t,m);}

Explanation after i finish golfing.

Example of use:

 int main(void){
 q("Plain");
 printf("\n");
 q("Nut");
 printf("\n"); 
 q("Chocolate");
 }

Joshpbarron

Posted 2015-08-03T22:58:22.033

Reputation: 787

3

CJam, 49 48 bytes

" ___
/""^  ^ ^. . ."S7*+6/rci=3/"\
|"*"|
\___/"

Try it online in the CJam interpreter.

How it works

" ___
/"

e# Push that string.

"^  ^ ^. . ."S7*+6/

e# Push that string, append 7 spaces and split into chunks of length 6.
e# This pushes ["^  ^ ^" ". . . " "      "].

rci

e# Read from STDIN, cast to character, then to integer.
e# "Plain", "Chocolate", "Nuts" -> 'P', 'C', 'N' -> 80, 67, 78

=

e# Select the corresponding element from the array.
e# Arrays wrap around in CJam, so for an array A of length 3,
e# A80= is A2=, A67= is A1= and A78= is A0=.

3/

e# Split into chunks of length 3.

"\
|"*

e# Join those chunks, using that string as separator.

"|
\___/"

e# Push that string.

At the end, CJam automatically prints all elements on the stack.

Dennis

Posted 2015-08-03T22:58:22.033

Reputation: 196 637

3

Javascript (ES6), 90

s=>" ___\n/"+(s.length-4?s.length-5?". .\\\n| . ":"   \\\n|   ":"^  \\\n|^ ^")+"|\n\\___/"

This is an anonymous arrow function. It uses the length of the input to determine which cookie to draw.

Explanation:

s=>
 " ___\n/" +               //build the first part of the cookie

 (s.length - 4 ?           //if the length is 4, this condition will evaluate to 0, which coerces to false. Otherwise it is true

      s.length - 5 ?            //if the length is 5, this evaluates to false; otherwise true

           ". .\\\n| . " :      //build the unique part of the Chocolate cookie, if length was not 5
           "   \\\n|   "        //build the unique part of the Plain cookie, if length was 5

      : "^  \\\n|^ ^"      //build the unique part of the Nuts cookie, if length was 4
 )

 + "|\n\\___/"             //build the last part of the cookie, and implicitly return the built string

To test:

f=s=>" ___\n/"+(s.length-4?s.length-5?". .\\\n| . ":"   \\\n|   ":"^  \\\n|^ ^")+"|\n\\___/"

console.log(f("Nuts"))
console.log(f("Plain"))
console.log(f("Chocolate"))

jrich

Posted 2015-08-03T22:58:22.033

Reputation: 3 898

3

BrainFuck, 481 447 436 bytes

Why not BrainFuck?, the program can probably be golfed more, but I think it's pretty neat.

,>++++++[-<---------->]<-------[----------->>>-<<+<[-->->+<<]]>>>+>>++.<+++++++++[->>>>>>>++++++++++<+++++<++++++++++++++<++++++++++<+++++<++++++++++<+++<]++++++++++>+++.>+++++...>++>++>-->+>++++<<<<<<<.<<<[->>>>>>.>>>.<<<<<.>>>>>.<<.<<<<.>>>>>.<<<<.>>>>>.<<<<<.>>>>.<<<<<.>>>>.<<...>.<<<<<<]>[->>>>>.<<...>>>.<<<<.>>>>>.<<<<...>>>>.<<<<<.>>>>.<<...>.<<<<<]>[->>>>.>>>>.<<<<<<..>>>.<<<<.>>>>>.>>.<<<<<<.>>>>>>.<<.<<<<<.>>>>.<<...>.<<<<]

AboveFire

Posted 2015-08-03T22:58:22.033

Reputation: 623

3

C# With indentation and line break

using System;
class Cookie
{
    static void Main()
    {
      String E="",N="",C=Console.ReadLine();
      if(C=="P"){E="   ";N="   ";}
      if(C=="C"){E=". .";N=" . ";}
      if(C=="N"){E="^  ";N="^ ^";}
      Console.Write(" ___ \n/" + E + "\\ \n|" + N + "|\n\\___/");
    }
}

Golfed (225 Characters)

using System;class X{static void Main(){String E="",N="",C=Console.ReadLine();if(C=="P"){E="   ";N="   ";}if(C=="C"){E=". .";N=" . ";}if(C=="N"){E="^  ";N="^ ^";}Console.Write(" ___ \n/" + E + "\\ \n|" + N + "|\n\\___/");}}[![enter image description here][1]][1]

Merin Nakarmi

Posted 2015-08-03T22:58:22.033

Reputation: 247

1Why not String C= Console.ReadLine(),E= ...etc? – rpax – 2015-08-14T21:03:34.197

Hey @rpax, Your comment helped me trim 2 characters. Thanks. – Merin Nakarmi – 2015-08-14T22:47:46.830

2

Pyth, 58 54 53 52 50 bytes

+d*\_3p+\/j"\\
|"c@["^  ^ ^"*". "3*d6)Chz3\|"\___/

I don't think this can be golfed more. I was really trying to squeze this in under 50 bytes

Downgoat

Posted 2015-08-03T22:58:22.033

Reputation: 27 116

A quick and easy one is ". . . " -> *". "3 – Sp3000 – 2015-08-05T06:51:36.070

If your interested, I found a nice method to do this in 42 bytes.

– Jakube – 2015-08-10T15:15:24.657

2

C# 6, 105 bytes

So very nearly got this sub-100 bytes, no idea where to squeeze the last few bytes from though :C

string C(string t)=>$" ___\n/{(t[0]=='C'?". .\\\n| . ":t[0]=='N'?"^  \\\n|^ ^":"   \\\n|   ")}|\n\\___/";

Sok

Posted 2015-08-03T22:58:22.033

Reputation: 5 592

2

JavaScript (ES6), 72 bytes

About as simple as it gets… new lines are counted as 1 byte each.

f=s=>` ___
/${s[4]?s[5]?`. .\\
| . `:`   \\
|   `:`^  \\
|^ ^`}|
\\___/`

Demo

As it is ES6, this demo only works in Firefox and Safari for now.

f=s=>` ___
/${s[4]?s[5]?`. .\\
| . `:`   \\
|   `:`^  \\
|^ ^`}|
\\___/`

// Snippet stuff

A.innerHTML = f("Nuts");
B.innerHTML = f("Plain");
C.innerHTML = f("Chocolate");
<p>Nuts</p>
<pre id=A></pre>

<p>Plain</p>
<pre id=B></pre>

<p>Chocolate</p>
<pre id=C></pre>

rink.attendant.6

Posted 2015-08-03T22:58:22.033

Reputation: 2 776

2

Commodore 64 BASIC, 181 bytes

10 INPUT A$
20 IF A$="PLAIN" THEN B$="/   \":C$="|   |"
30 IF A$="CHOCOLATE" THEN B$="/. .\":C$="| . |"
40 IF A$="NUTS" THEN C$="/^  \":C$="|^ ^|"
50 PRINT" ___":PRINT B$:PRINT C$:PRINT"\___/"

Notes:

Instead of backslashes \ the SHIFT-M characters have been used, for slashes / - SHIFT-N and for pipes | - SHIFT-T. SHIFT-Z (card diamond sign) was used for ^. In fact characters do not matter as they all occupy one byte each.

Because on C64 each command (PRINT, INPUT, THEN, etc.) takes 2 bytes in memory (or some even one, IIRC), the BASIC language was worth trying (however, it took more bytes than I expected).

The program size was calculated by measuring free memory before typing the program (38909 bytes) and after (38728 bytes), using PRINT FRE(0)+65536 command, giving 181 bytes of difference.

Code tested and screenshots prepared with this tool: http://codeazur.com.br/stuff/fc64_final/ (GPL).

Screenshots:

C64 screenshot 1

C64 screenshot 2

Voitcus

Posted 2015-08-03T22:58:22.033

Reputation: 755

2

Lua 5.3, 113 Bytes 112 Bytes

c=io.read()print(' ___\n/'..(c=='plain'and'   \\\n|   'or c=='nut'and'^  \\\n|^ ^'or'. .\\\n| . ')..'|\n\\___/')

It uses a lot of the ternary operator and string concatenation, and I squeezed out all whitespace that isn't part of the output itself.

Muddmaker

Posted 2015-08-03T22:58:22.033

Reputation: 21

2

Java 258 217 characters/bytes


Golfed

class C{public static void main(String[] a){p(" ___");if(a[0].equals("Chocolate")) {p("/. .\\");p("| . |");}if(a[0].equals("Nut")){p("/^  \\");p("|^ ^|");}p("\\___/");}static void p(String s) {System.out.println(s);}}

Original

class C {
    public static void main(String[] a) {
        p(" ___");
        if(a[0].equals("Chocolate")) {
            p("/. .\\");
            p("| . |");
        }
        if(a[0].equals("Nut")){
            p("/^  \\");
            p("|^ ^|");
        }
        p("\\___/");
    }
    static void p(String s) {
        System.out.println(s);
    }
}

OverCoder

Posted 2015-08-03T22:58:22.033

Reputation: 131

1

LUA 270 characters 270 bytes

    c = io.read()
    if c == "plain" then
    print" ___"
    print"/   \\"
    print"|   |"
    print"\\___/"
    io.read()
    elseif c == "chocolate" then
    print" ___"
    print"/. .\\"
    print"| . |"
    print"\\___/"
    io.read()
    elseif c == "nut" then
    print" ___"
    print"/^  \\"
    print"|^ ^|"
    print"\\___/"
    end

Alex Allen

Posted 2015-08-03T22:58:22.033

Reputation: 91

this is my second answer to any challenges – Alex Allen – 2015-08-10T22:26:09.920

The "What type of cookie do you want" can be taken out, it isn't needed. That takes out 39 bytes right there. – The_Basset_Hound – 2015-08-10T22:39:45.230

This is a code-golf challenge. Try to shorten your code a little. E.g. You don't need the initial print, shorten cookie to c, remove whitespace during ifs, remove those unnecessary io.read()s, the first and last line of a cookie is always the same, .... – Jakube – 2015-08-11T10:33:39.647

@BassetHound removed the print statement – Alex Allen – 2015-08-11T10:43:24.427

@Jakube shortened cookie to c – Alex Allen – 2015-08-11T10:43:57.560

How about this. Only 173 bytes using exactly the same approach.

– Jakube – 2015-08-11T10:52:42.840

Or even this for 143 by using a trick of the Tips for golfing in lua-page.

– Jakube – 2015-08-11T10:56:50.103

1

LOLCODE 265 characters

HAI
I HAS A T
GIMMEH T
VISIBLE " ___"
BOTH SAEM T AN "Chocolate", O RLY?
YA RLY
VISIBLE "/. .\"
VISIBLE "| . |"
OIC
BOTH SAEM T AN "Nut", O RLY?
YA RLY
VISIBLE "/^ ^\"
VISIBLE "|^  |"
OIC
BOTH SAEM T AN "Plain", O RLY?
YA RLY
VISIBLE "/   \"
VISIBLE "|   |"
OIC
VISIBLE "\___/"
KTHXBYE

Run

OverCoder

Posted 2015-08-03T22:58:22.033

Reputation: 131