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.
-
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\$Level River St– Level River St2015年08月03日 23:07:18 +00:00Commented 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\$Reto Koradi– Reto Koradi2015年08月03日 23:14:39 +00:00Commented 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\$Level River St– Level River St2015年08月03日 23:21:09 +00:00Commented Aug 3, 2015 at 23:21
-
1\$\begingroup\$ Will functions be permitted? \$\endgroup\$bren– bren2015年08月04日 01:39:38 +00:00Commented 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\$manatwork– manatwork2015年08月04日 10:36:38 +00:00Commented Aug 4, 2015 at 10:36
16 Answers 16
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.
-
\$\begingroup\$ What do you think the
ordmethod 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\$manatwork– manatwork2015年08月04日 06:32:46 +00:00Commented Aug 4, 2015 at 6:32 -
\$\begingroup\$ Forget it. Once again
trproves to be shorter:->s{' ___↵/% \↵|% %|↵\___/'.tr ?%,'^. '[s.ord%3]}\$\endgroup\$manatwork– manatwork2015年08月04日 06:39:02 +00:00Commented 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 usedtror%before. \$\endgroup\$Level River St– Level River St2015年08月04日 10:21:42 +00:00Commented 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\$manatwork– manatwork2015年08月04日 10:28:23 +00:00Commented Aug 4, 2015 at 10:28
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 . "]
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.
-
\$\begingroup\$ Optional arguments are most certainly allowed \$\endgroup\$Beta Decay– Beta Decay2015年08月04日 19:42:53 +00:00Commented Aug 4, 2015 at 19:42
-
\$\begingroup\$ "Use any means to do so." Yes, optional arguments are allowed. \$\endgroup\$The_Basset_Hound– The_Basset_Hound2015年08月04日 19:44:07 +00:00Commented 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\$xenia– xenia2015年08月04日 19:46:48 +00:00Commented 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\$xenia– xenia2015年08月04日 19:58:14 +00:00Commented Aug 4, 2015 at 19:58
-
3\$\begingroup\$ @Loovjo Using strange and unusual methods are the whole spirit of code golf :) \$\endgroup\$Beta Decay– Beta Decay2015年08月04日 20:01:23 +00:00Commented Aug 4, 2015 at 20:01
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");
}
-
\$\begingroup\$ 105 bytes \$\endgroup\$ceilingcat– ceilingcat2020年07月13日 02:04:14 +00:00Commented Jul 13, 2020 at 2:04
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.
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"))
BrainFuck, (削除) 481 (削除ここまで) (削除) 447 (削除ここまで) 436 bytes
Why not BrainFuck?, the program can probably be golfed more, but I think it's pretty neat.
,>++++++[-<---------->]<-------[----------->>>-<<+<[-->->+<<]]>>>+>>++.<+++++++++[->>>>>>>++++++++++<+++++<++++++++++++++<++++++++++<+++++<++++++++++<+++<]++++++++++>+++.>+++++...>++>++>-->+>++++<<<<<<<.<<<[->>>>>>.>>>.<<<<<.>>>>>.<<.<<<<.>>>>>.<<<<.>>>>>.<<<<<.>>>>.<<<<<.>>>>.<<...>.<<<<<<]>[->>>>>.<<...>>>.<<<<.>>>>>.<<<<...>>>>.<<<<<.>>>>.<<...>.<<<<<]>[->>>>.>>>>.<<<<<<..>>>.<<<<.>>>>>.>>.<<<<<<.>>>>>>.<<.<<<<<.>>>>.<<...>.<<<<]
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]
-
1\$\begingroup\$ Why not
String C= Console.ReadLine(),E=...etc? \$\endgroup\$rpax– rpax2015年08月14日 21:03:34 +00:00Commented Aug 14, 2015 at 21:03 -
\$\begingroup\$ Hey @rpax, Your comment helped me trim 2 characters. Thanks. \$\endgroup\$Merin Nakarmi– Merin Nakarmi2015年08月14日 22:47:46 +00:00Commented Aug 14, 2015 at 22:47
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\\___/";
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
-
\$\begingroup\$ A quick and easy one is
". . . " -> *". "3\$\endgroup\$Sp3000– Sp30002015年08月05日 06:51:36 +00:00Commented Aug 5, 2015 at 6:51 -
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>
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:
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.
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);
}
}
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
-
\$\begingroup\$ this is my second answer to any challenges \$\endgroup\$Alex Allen– Alex Allen2015年08月10日 22:26:09 +00:00Commented 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\$The_Basset_Hound– The_Basset_Hound2015年08月10日 22:39:45 +00:00Commented 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
cookietoc, remove whitespace during ifs, remove those unnecessaryio.read()s, the first and last line of a cookie is always the same, .... \$\endgroup\$Jakube– Jakube2015年08月11日 10:33:39 +00:00Commented Aug 11, 2015 at 10:33 -
\$\begingroup\$ @BassetHound removed the print statement \$\endgroup\$Alex Allen– Alex Allen2015年08月11日 10:43:24 +00:00Commented Aug 11, 2015 at 10:43
-
\$\begingroup\$ @Jakube shortened cookie to c \$\endgroup\$Alex Allen– Alex Allen2015年08月11日 10:43:57 +00:00Commented Aug 11, 2015 at 10:43
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