16
\$\begingroup\$

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.

asked Aug 3, 2015 at 22:58
\$\endgroup\$
9
  • 7
    \$\begingroup\$ 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 meta.codegolf.stackexchange.com/q/2140/15599 then repost later. \$\endgroup\$ Commented Aug 3, 2015 at 23:07
  • 1
    \$\begingroup\$ Allowing 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? \$\endgroup\$ Commented Aug 3, 2015 at 23:14
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$ Commented Aug 3, 2015 at 23:21
  • 1
    \$\begingroup\$ Will functions be permitted? \$\endgroup\$ Commented Aug 4, 2015 at 1:39
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 4, 2015 at 10:36

16 Answers 16

7
\$\begingroup\$

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.

answered Aug 4, 2015 at 3:57
\$\endgroup\$
4
  • \$\begingroup\$ 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]} \$\endgroup\$ Commented Aug 4, 2015 at 6:32
  • \$\begingroup\$ Forget it. Once again tr proves to be shorter: ->s{' ___↵/% \↵|% %|↵\___/'.tr ?%,'^. '[s.ord%3]} \$\endgroup\$ Commented Aug 4, 2015 at 6:39
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Aug 4, 2015 at 10:21
  • \$\begingroup\$ 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.) \$\endgroup\$ Commented Aug 4, 2015 at 10:28
4
\$\begingroup\$

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 . "]
answered Aug 10, 2015 at 15:11
\$\endgroup\$
4
\$\begingroup\$

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.

answered Aug 4, 2015 at 19:17
\$\endgroup\$
10
  • \$\begingroup\$ Optional arguments are most certainly allowed \$\endgroup\$ Commented Aug 4, 2015 at 19:42
  • \$\begingroup\$ "Use any means to do so." Yes, optional arguments are allowed. \$\endgroup\$ Commented Aug 4, 2015 at 19:44
  • \$\begingroup\$ Okay, but I think I'll still stick with the first one and leave the second one as it is. \$\endgroup\$ Commented Aug 4, 2015 at 19:46
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Aug 4, 2015 at 19:58
  • 3
    \$\begingroup\$ @Loovjo Using strange and unusual methods are the whole spirit of code golf :) \$\endgroup\$ Commented Aug 4, 2015 at 20:01
3
\$\begingroup\$

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");
 }
answered Aug 4, 2015 at 19:26
\$\endgroup\$
1
  • \$\begingroup\$ 105 bytes \$\endgroup\$ Commented Jul 13, 2020 at 2:04
3
\$\begingroup\$

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.

answered Aug 4, 2015 at 20:16
\$\endgroup\$
3
\$\begingroup\$

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"))
answered Aug 5, 2015 at 7:10
\$\endgroup\$
3
\$\begingroup\$

BrainFuck, (削除) 481 (削除ここまで) (削除) 447 (削除ここまで) 436 bytes

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

,>++++++[-<---------->]<-------[----------->>>-<<+<[-->->+<<]]>>>+>>++.<+++++++++[->>>>>>>++++++++++<+++++<++++++++++++++<++++++++++<+++++<++++++++++<+++<]++++++++++>+++.>+++++...>++>++>-->+>++++<<<<<<<.<<<[->>>>>>.>>>.<<<<<.>>>>>.<<.<<<<.>>>>>.<<<<.>>>>>.<<<<<.>>>>.<<<<<.>>>>.<<...>.<<<<<<]>[->>>>>.<<...>>>.<<<<.>>>>>.<<<<...>>>>.<<<<<.>>>>.<<...>.<<<<<]>[->>>>.>>>>.<<<<<<..>>>.<<<<.>>>>>.>>.<<<<<<.>>>>>>.<<.<<<<<.>>>>.<<...>.<<<<]
answered Aug 10, 2015 at 14:42
\$\endgroup\$
3
\$\begingroup\$

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]
answered Aug 11, 2015 at 0:10
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why not String C= Console.ReadLine(),E= ...etc? \$\endgroup\$ Commented Aug 14, 2015 at 21:03
  • \$\begingroup\$ Hey @rpax, Your comment helped me trim 2 characters. Thanks. \$\endgroup\$ Commented Aug 14, 2015 at 22:47
2
\$\begingroup\$

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\\___/";
answered Aug 5, 2015 at 14:14
\$\endgroup\$
2
\$\begingroup\$

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

answered Aug 5, 2015 at 6:07
\$\endgroup\$
2
  • \$\begingroup\$ A quick and easy one is ". . . " -> *". "3 \$\endgroup\$ Commented Aug 5, 2015 at 6:51
  • \$\begingroup\$ If your interested, I found a nice method to do this in 42 bytes. \$\endgroup\$ Commented Aug 10, 2015 at 15:15
2
\$\begingroup\$

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>

Downgoat
29.2k6 gold badges85 silver badges157 bronze badges
answered Aug 5, 2015 at 15:16
\$\endgroup\$
2
\$\begingroup\$

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

answered Aug 11, 2015 at 12:46
\$\endgroup\$
2
\$\begingroup\$

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.

answered Aug 11, 2015 at 22:07
\$\endgroup\$
2
\$\begingroup\$

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);
 }
}
answered Aug 13, 2015 at 20:18
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
7
  • \$\begingroup\$ this is my second answer to any challenges \$\endgroup\$ Commented Aug 10, 2015 at 22:26
  • \$\begingroup\$ The "What type of cookie do you want" can be taken out, it isn't needed. That takes out 39 bytes right there. \$\endgroup\$ Commented Aug 10, 2015 at 22:39
  • \$\begingroup\$ 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, .... \$\endgroup\$ Commented Aug 11, 2015 at 10:33
  • \$\begingroup\$ @BassetHound removed the print statement \$\endgroup\$ Commented Aug 11, 2015 at 10:43
  • \$\begingroup\$ @Jakube shortened cookie to c \$\endgroup\$ Commented Aug 11, 2015 at 10:43
1
\$\begingroup\$

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

answered Aug 13, 2015 at 21:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.