31
\$\begingroup\$

The mid-autumn festival has begun!

Unfortunately, all my mooncakes were stolen -- they're getting too expensive for small folk such as myself, and I fear I won't be able to eat any this year!

So I turn to you for help. Would you be able to make me some?

For those unaware, let me educate you on what a mooncake looks like.


Mooncakes come in many different sizes!
So I'm gonna give you my input, n, when I want one.

Here are some examples of the output I want:

Mooncake of size n = 3:

    (@@@@@)  
    (@MAF@) 
    (@@@@@) 

Mooncake of size n = 6:

    (@@@@@@@@@@@) 
    (@         @)
    (@    M    @) 
    (@    F    @)
    (@         @)
    (@@@@@@@@@@@) 

That is to say, a mooncake of size n is:

  • n lines high
  • 2n - 1 @s long
  • 2n + 1 characters long (@s and brackets)

And you better not throw me your too-tiny practice mooncakes!
Assume input will always be n >= 3.

Mooncakes also contain one of the following decorations:

  • MF
  • MAF
  • HMF
  • JCJ
  • TTT
  • ZJ
  • LF
  • RF
  • CF

Which one, it doesn't matter - as long as it is vertically and horizontally centered.
It can be written vertically or horizontally too!

I want variety!
If you're really going to make me two of the same mooncake, the decoration better be different!

That is, multiple executions of your program with the exact same input must not always yield the same decoration.

I can't wait to eat your moon cakes, so the sooner I can receive them (the shorter your code) the better!

Good Luck!


For those wondering about the decorations:
They are the initials of all alternative names for the Mid-Autumn Festival.
A list can be found in the Wikipedia page linked at the top of this post.


Clarifications:

There are no rules regarding leading and trailing whitespace.
Have as much or as little as you like!

The decorations must be in the very center of your mooncake!
On horizontal decorations, this means it must be on the middle line of your cake, and the number of characters to the left and right of the decoration string must be equal.
On vertical decorations, this means it must reside in the middle column of your cake, and the number of characters above and below the decoration must be equal.

Not all decorations must be used!
The only requirement is that there must be more than one possibility for any given input n. The possibilities also do not need to be even.

Functions are acceptable.

\$\endgroup\$
  • 2
    \$\begingroup\$ Hi, welcome to PPCG! This looks like a great first post. usually we recommend to use the Sandbox for proposed challenges. There you can get feedback from others, and perfect the challenge with things you might have not thought about, before posting it here. That being said, your challenge looks well-thought-out. +1 from me. One question regarding your first example though, why does it read MAF and not MF? I don't see an A in your decoration-options. \$\endgroup\$ – Kevin Cruijssen Sep 15 '16 at 7:12
  • 1
    \$\begingroup\$ Thanks! Sorry, I wasn't aware of the sandbox. Am I able to leave just this one up anyway? About the decoration - that was a mistake. I added MAF to the allowed decorations. Feedback very much appreciated! \$\endgroup\$ – Moon Rabbit Sep 15 '16 at 7:13
  • 1
    \$\begingroup\$ There are no rules regarding leading and trailing spaces. Up to you! \$\endgroup\$ – Moon Rabbit Sep 15 '16 at 8:26
  • 3
    \$\begingroup\$ Hey, I need to manually type all this into my mooncake machine to get the actual mooncakes! \$\endgroup\$ – Moon Rabbit Sep 16 '16 at 1:16
  • 4
    \$\begingroup\$ ... in which case it's good that none of those contrived languages with non-ASCII character sets have reared their head, because they are a pain to type :) \$\endgroup\$ – GreenAsJade Sep 16 '16 at 6:34

12 Answers 12

9
\$\begingroup\$

Pyth, 71 65 59 58 bytes

Saved 1 byte thanks to @StevenH.

jjRc"(@@)"2++J*KtytQ\@m.[Kd;.[ttQ@,,O"MC"\F]Oc"MAFHMF"3QkJ

Try it online. Test suite.

So much padding.

\$\endgroup\$
  • 2
    \$\begingroup\$ It's nuts how different this answer is from the one below it in the same language... \$\endgroup\$ – Magic Octopus Urn Sep 15 '16 at 17:50
  • \$\begingroup\$ You can save one byte by replacing ?%Q2 with @, (reverse the order of the two choices)...Q. I (ab)used that a lot in golfing my own Pyth answer down. \$\endgroup\$ – Steven H. Sep 20 '16 at 4:55
  • \$\begingroup\$ @StevenH. Thanks, I always forget about modular indexing. \$\endgroup\$ – PurkkaKoodari Sep 20 '16 at 10:36
7
\$\begingroup\$

JavaScript ES6, 206 196 192 188 187 180 176 169 165 156 bytes

g=(n,r=new Date%2,a=(s,x,b=' @'[+!s].repeat(n-3+!x))=>`(@${b+(s||'@')+b}@)
`,l=a` `.repeat(n/2-2+n%2))=>a()+l+(n%2?a(r?'MAF':'HMF',1):a('RC'[r])+a`F`)+l+a()

Breakdown

r=new Date%2                           // Sometimes 0, sometimes 1

// Function to create lines of the cake of the form `(@@@@@@@@@)` or `(@  ${s}  @)`
a=(s,x,b=' @'[+!s].repeat(n-3+!x))=>`(@${b+(s||'@')+b}@)
`

// Cake parts
a()                                    // Top of the cake.
l=a` `.repeat(n/2-2+n%2)               // Undecorated inner part.
a(r?'MAF':'HMF',1)                     // Decoration for uneven sized cakes.
a('RC'[r])+a`F`                        // Decoration for even sized cakes.
l                                      // Undecorated inner part.
a()                                    // Bottom part.

My first attempt at code-golf. This can probably be golfed more.

saved 4 bytes thanks to @ETHProductions

Edit I took the liberty of using Date.now()%2 new Date%2 to satisfy:

multiple executions of your program with the exact same input must not always yield the same decoration

this allows me to save another 7 bytes over +Math.random()>.5

saved another 4 bytes thanks to @Arnauld

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG, and great first answer! I believe ['MA','HA'][r]+'F' can be golfed to 'MH'[r]+'AF'. \$\endgroup\$ – ETHproductions Sep 15 '16 at 14:03
  • 1
    \$\begingroup\$ You can save two bytes by replacing [' R ',' C '][r] with " ${'RC'[r]} ", replacing each " with a backtick. \$\endgroup\$ – ETHproductions Sep 15 '16 at 14:11
  • 1
    \$\begingroup\$ And ['MA','HM'][r]+'F' is actually two bytes longer than ['MAF','HMF'][r] ;) \$\endgroup\$ – ETHproductions Sep 15 '16 at 14:12
  • 2
    \$\begingroup\$ You can use ' @'[+!s] instead of (s?' ':'@') \$\endgroup\$ – Arnauld Sep 15 '16 at 17:17
  • 2
    \$\begingroup\$ Also, you can use new Date%2 instead of Date.now()%2. \$\endgroup\$ – Arnauld Sep 15 '16 at 17:27
6
\$\begingroup\$

Pyth, 99 79 71 68 64 bytes

Pyth is very bad all right at making strings. Or maybe I'm just bad getting better at golfing them.

jmj.[-yQ3@,k@,@,O"MC"\FgydQOc"MAFHMF"3Q>2ahydQ@" @"sIcdtQc2"(@@)

Can create the decorations MAF and HMF horizontally, and the decorations MF and CF vertically.

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Ahh, good to see some character development in this story. \$\endgroup\$ – Moon Rabbit Sep 16 '16 at 6:37
6
\$\begingroup\$

Vim, 118 bytes

Takes input as a buffer (e.g. a file with the number n as its contents).

"aDi()<Esc>@ai@<Esc>.xY@apddll<C-v>G$k3hr @=@a/2-1
j@=@a-2-@a%2
l:let r=abs(reltime()[1])%2
@=@a%2?r?"RJCJ":"3rT":"rFkr"."ML"[r]
<Esc>

Here it is with the unprintable control characters in xxd format:

0000000: 2261 4469 2829 1b40 6169 401b 2e78 5940  "aDi().@ai@..xY@
0000010: 6170 6464 6c6c 1647 246b 3368 7220 403d  apddll.G$k3hr @=
0000020: 4061 2f32 2d31 0a6a 403d 4061 2d32 2d40  @a/2-1.j@=@a-2-@
0000030: 6125 320a 6c3a 6c65 7420 723d 6162 7328  a%2.l:let r=abs(
0000040: 7265 6c74 696d 6528 295b 315d 2925 320a  reltime()[1])%2.
0000050: 403d 4061 2532 3f72 3f22 524a 434a 223a  @=@a%2?r?"RJCJ":
0000060: 2233 7254 223a 2272 466b 7222 2e22 4d4c  "3rT":"rFkr"."ML
0000070: 225b 725d 0a1b                           "[r]..

Try it online! (As it turns out, the V interpreter works fine for normal Vim code, too.)

Explanation

"aD                   " Delete the number and store it in @a
i()<Esc>              " Insert empty parentheses
@ai@<Esc>.x           " Insert @a '@' characters between the parentheses twice; delete 1
Y@apdd                " Copy the line and paste it @a times; delete 1
ll<C-v>G$k3hr<Space>  " Replace the inner area with spaces
@=@a/2-1<CR>j         " Go down @a/2-1 lines
@=@a-2-@a%2<CR>l      " Go right @a-2-@a%2 columns
:let r=reltime()[1]%2<CR>  " Get a random 1 or 0 based on the time (this may be OS-dependent)
@=@a%2?
   r?"RJCJ":"3rT"     " For odd @a, replace the next 3 characters with "JCJ" or "TTT"
  :"rFkr"."ML"[r]     " For even @a, replace this character with "F" and the above with "M" or "L"
<CR><Esc>
\$\endgroup\$
  • \$\begingroup\$ Wow, nice! I think this is the first non-deterministic vim answer I've seen! A couple thoughts: 1) When you put the input in "arguments" rather than "input", it predefines @a to arg1, @b to arg2, etc. but this is a V specific feature. This doesn't technically matter since you have "aD at the beginning, but I just thought I'd point it out. 2) You could take one byte off if you did dd@ap instead of Y@apdd \$\endgroup\$ – DJMcMayhem Sep 15 '16 at 19:52
  • \$\begingroup\$ @DJMcMayhem dd@ap doesn't quite work alas. \$\endgroup\$ – Jordan Sep 15 '16 at 21:14
  • \$\begingroup\$ dd@aP works, but then requires an additional j and an additional k down the line. \$\endgroup\$ – Jordan Sep 15 '16 at 21:16
5
\$\begingroup\$

PHP, 342 292 249 185 178 176 bytes

for(;$i<$n=$argv[1];)$o.=str_pad("(@",2*$n-1," @"[$i++%($n-1)<1])."@)
";$p=F.[M,AM,R,MH][rand()&2|$d=$n&1];$f=$n*($n+$d)-2;for($i=2+$d;$i--;$f+=$d?:2*$n+2)$o[$f]=$p[$i];echo$o;

Call with php -r '<code>' <size>

history

Rev 1: initial version; all sizes (including tiny cakes), all decorations, all possible directions

Rev. 2: removed tiny cakes (-36 bytes), restructured decoration options, removed one decoration item (-21) and a single byte golf (-1).

Rev. 3: Down to four decorations; (-17), only horizontal for odd sizes (-18) plus minor golfing (-8).

Rev. 4: Thanks to Jörg for golfing down the "paint cake" part; he took off an amazing (-31).
Another -6 with my additional golfing, and -27 for using a single string instead of an array of strings.

Rev. 5: -7 bytes mostly thanks to Christallkeks

breakdown

This is getting slimmer by the hour. :)

// paint cake
for(;$i<$n=$argv[1];)$o.=str_pad("(@",2*$n-1," @"[$i++%($n-1)<1])."@)\n";

// add deco
$p=F.[M,AM,R,MH][rand()&2|$d=$n&1];
$f=$n*($n+$d)-2;
for($i=2+$d;$i--;$f+=$d?:2*$n+2)$o[$f]=$p[$i];

// output
echo$o;
\$\endgroup\$
  • 1
    \$\begingroup\$ for($i=0;$i<$n=$argv[1];$i++){$o[]=str_pad("(@",2*$n-1,!$i|$i==$n-1?"@":" ")."@)";} \$\endgroup\$ – Jörg Hülsermann Sep 15 '16 at 12:22
  • \$\begingroup\$ @JörgHülsermann: Lots of thanks. I had to take a similar expression for !$i|$‌​i==$n-1?"@":" "; the ternary would just not accept yours (on my machine), although it is correct. \$\endgroup\$ – Titus Sep 15 '16 at 17:30
  • \$\begingroup\$ try it for example on sandbox.onlinephpfunctions.com \$\endgroup\$ – Jörg Hülsermann Sep 15 '16 at 17:33
  • 1
    \$\begingroup\$ This saves you six bytes on the cake paint: for(;$i<$n=$argv[1];)$o.=str_pad("(@",2*$n-1," @"[$i++%($n-1)==0])."@)\n"; Also, it looks like you got "MAD" golfing your decos ;-) \$\endgroup\$ – Christallkeks Sep 16 '16 at 15:00
4
\$\begingroup\$

Java 7, 399 349 bytes

Updated version with help from @Dodge and @Kevin Cruijssen:

void m(int n){int i=2,r=n%2,x=2*n,u=r+2,y=r*4+(int)(Math.random()*2)*u,z=y+u;String t="MFZJMAFHMF".substring(y,z);char[][]c=new char[n][x+1];while(i<x-1)c[0][i]=c[n-1][i++]=64;for(i=0;i<u;)c[(n-1)/2+(1-r)*i][r*(i-1)+n]=t.charAt(i++);for(i=0;i<n;){c[i][0]=40;c[i][1]=c[i][x-1]=64;c[i][x]=41;System.out.println(new String(c[i++]).replace('\0',' '));}}

void m(int n){String[]s={"MF","MAF","ZJ","HMF","LF","JCJ","RF","TTT","CF","MAF"};char[]d=s[((int)(Math.random()*5))*2+(n%2)].toCharArray(),c[]=new char[n][2*n+1];int i=2;while(i<2*n-1)c[0][i]=c[n-1][i++]='@';i=0;while(i<d.length)c[(n-1)/2+(1-(n%2))*i][(n%2)*(-1+i)+n]=d[i++];i=0;while(i<n){c[i][0]='(';c[i][1]=c[i][2*n-1]='@';c[i][2*n]=')';System.out.println(new String(c[i++]).replace('\0',' '));}}

Try it here!

The new version is much more optimized and got rid of the String array handling. Also as suggested, there are only 4 decorations now: 2 for even inputs (MF,ZJ) and 2 for odd inputs (MAF,HMF) which are combined into a single String.

Ungolfed:

void m(int n){
    int i=2,
    r=n%2,
    x=2*n,
    u=r+2, // length of the decoration string
    y=r*4+(int)(Math.random()*2)*u, // random starting index of string (0, 2, 4, 7)
    z=y+u; // exclusive end index of string (2, 4, 7, 10)
    String t="MFZJMAFHMF".substring(y,z);
    char[][]c=new char[n][x+1];
    while(i < x-1) {
        c[0][i]=c[n-1][i++]=64; // '@'
    }
    for(i=0; i<u;) {
        c[(n-1)/2+(1-r)*i][r*(i-1)+n]=t.charAt(i++); // Depending on even/odd, fills the center column/row respectively with the decoration
    }
    for(i=0; i<n;) {
        c[i][0]=40; // '('
        c[i][1]=c[i][x-1]=64; // '@'
        c[i][x]=41; // ')'
        System.out.println(new String(c[i++]).replace('\0',' ')); // Print all
    }
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for the submission! Let me clarify, as a few users have missed it - not all decorations must be included. I have gone ahead and bolded this in the 'clarifications' section. Hopefully that will save you a few bytes! I believe you would still be able to use the same method to select decorations. \$\endgroup\$ – Moon Rabbit Sep 15 '16 at 15:29
  • 4
    \$\begingroup\$ Haha, as far as Java golfing goes, he might as well include all of the decorations ;). +1, always, for anyone with the balls to whip out the Java-putter. \$\endgroup\$ – Magic Octopus Urn Sep 15 '16 at 17:52
  • \$\begingroup\$ Why is "MAF" repeated? \$\endgroup\$ – Cyoce Sep 15 '16 at 20:52
  • \$\begingroup\$ @carusocomputing Very true. Just in case, though, I thought I'd mention it. Nice! \$\endgroup\$ – Moon Rabbit Sep 16 '16 at 1:32
  • \$\begingroup\$ Thanks for your feedback :) True, Java isn't very optimal for golfing, but it's pretty fun to do it :P. Maybe I could use the loops and initialization of the decoration char[] more efficiently and golf other things further. @Cyoce I duplicated MAF because then I have all decorations for an even input at indices 0,2,4,6,8 and all decorations for an odd input at indices 1,3,5,7,9. That makes it easier to calculate the random index for a given input. Math.random()*5 gives a random number from 0-4. *2 spreads it to 0,2,4,6,8. +n%2 adds 1 for odd inputs to get 1,3,5,7,9. \$\endgroup\$ – QBrute Sep 16 '16 at 8:26
3
\$\begingroup\$

Batch, 386 bytes

@echo off
set/pn=
set f=HMAC
set/ao=n%%2,u=n/2,l=h=u+1,d=%random%%%2*2+1
if %o%==1 set/al=u=0,d/=2
set c=
for /l %%i in (4,1,%n%) do call set c= %%c%%
call:l %n%
for /l %%i in (2,1,%n%) do call:l %%i
exit/b
:l
set s=   
if %1==%h% call set s=%%f:~%d%,2%%F
if %1==%u% call set s= %%f:~%d%,1%% 
if %1==%l% set s= F 
set s=(@%c%%s%%c%@)
if %1==%n% set s=%s: =@%
echo %s%

Will only output HMF, MAF, MF or CF as appropriate. Note: certain lines end in white space. Variables:

  • n Input parameter (read from STDIN)
  • f Decoration prefixes (suffix F is implied)
  • o Oddness of n (only used once, but if statements don't accept expressions)
  • l Row of the upper vertical character, or 0 for a horizontal decoration
  • u Row of the lower vertical character, or 0 for a horizontal decoration
  • h Row of the horizontal decoration (gets overwritten by a vertical decoration)
  • d Index of decoration in decoration prefix (0/1 for horizontal or 1/3 for vertical)
  • c String of n-3 spaces
  • s Output string for each row
  • %1 Row number, but set to n for the first row too, so that both first and last rows use @s instead of spaces.
\$\endgroup\$
3
\$\begingroup\$

C, 233 Bytes

Should be able to golf this down a bit from here...

A="HMFMAFCF";i,j,k,t;f(n){t=time();char*S=n&1?t&1?A:A+3:t&1?A+1:A+6;for(;i<n;i++,puts(")"))for(j=0,k=2*n-1,putchar(40);j<k;putchar(0==i*j|i==n-1|j==k-1?64:n&1&i==n/2&j>n-3&j<n+1?*S++:n&1?32:(i==n/2-1|i==n/2)&j>n-2&j<n?*S++:32),j++);}

Great challenge, this was difficult and ugly to code.

Run with this main func;

main(c,v)char**v;
{
    f(atoi(v[1]));
}
\$\endgroup\$
2
\$\begingroup\$

Ruby 2.3.1, 449 265 245 233 230 characters

Seems like there should be a ruby answer, so here is a ruby answer. It's really not that clever, Hopefully someone else here will be more clever ;)

Golfed version:

def m(n)
d,s=n.odd?? [[%w{MAF HMF}.sample],n/2]:[%w{RF LF}.sample.chars,(n-2)/2]
r=->f{l=f.size;q=($i-l)/2;($c=' '*$i)[q...l+q]=f;puts "(@#$c@)"}
$i=2*n-1;a='@'*$i
r[a]
(1..n-2).map{|x|(s...s+d.size)===x ?r[d[x-s]]:r['']}
r[a]
end

Golfing tricks:

  • replace method declaration with a stabby string interpolation of
  • $globals doesn't need #{global}, only #$global
  • === for ranges is shorter than .covers?

Readable version

def row(inner_width, fillchar='')
  padding = ( inner_width - fillchar.size) / 2
  (center =(' ' * inner_width))[padding...fillchar.size+padding]=fillchar
  puts "(@"+center+"@)"
end

def mooncake(n)
  decoration = n.odd?? [%w{ MAF HMF JCJ TTT }.sample] : %w{ ZJ LF RF CF }.sample.chars
  start_row = n.odd?? (n/2) : (n - 2) / 2
  inner_width = 2 * n - 1
  row(inner_width,'@'*inner_width)
  (1...(n-1)).each do |row|
    if (start_row ... start_row + decoration.size).include? row 
      row(inner_width,decoration[row - start_row])      
    else
      row(inner_width)      
    end
  end
  row(inner_width,'@'*inner_width)
end

Testing

mooncake(3)
mooncake(4)
mooncake(5)
mooncake(6)
\$\endgroup\$
  • \$\begingroup\$ I reimplemented w/o using the array, seems like everyone is doing that. \$\endgroup\$ – Rob Sep 18 '16 at 15:10
1
\$\begingroup\$

I was bored ... here are two more versions:

PHP, 193 bytes

function p($s){global$n;return"(@".str_pad($s,2*$n-3,$s?" ":"@",2)."@)
";}$p=[M,MA,R,HM][rand()&2|1&$n=$argv[1]];echo p(""),$e=str_repeat(p(" "),($n+$n%2)/2-2),$n&1?p($p.F):p($p).p(F),$e,p("");

a port of Lmis´ answer

PHP, 191 bytes

for($p=[M,MA,R,HM][rand()&2|1&$n=$argv[1]].F;$i<$n*$w=2*$n+1;$i++)echo($x=$i%$w)?$w-1-$x?($y=$i/$w|0)%($n-1)&&1-$x&&$w-2-$x?$p[$n&1?$n>>1!=$y?9:$x-$n+1:($n-$x?9:$y-$n/2+1)]?:" ":"@":")
":"(";

printing the cake character by character in a single loop

breakdown

for(
    $p=[M,MA,R,HM][rand()&2|1&$n=$argv[1]].F;   // pick decoration
    $i<$n*$w=2*$n+1;$i++)       // loop $i from 0 to $n*width-1:
echo                                // print ...
    $w-1-($x=$i%$w)                 // 1. not last column
        ?$x                         // 2. not first column
            ?
                ($y=$i/$w|0)%($n-1) // 3. not first or last line
                && 1-$x%($w-3)      // and not second or (width-2)th column
                ?$p[$n&1
                    ?$n>>1!=$y?3:1+$x-$n
                    :($n-$x?3:1+$y-$n/2)
                ]   ?               // 4. decoration character
                    :" "            // 4. else: blank
                :"@"                // 3. else: "@"
            :"("                    // 2. else: "("
        :")\n"                      // 1. else: ")"+newline
    ;
\$\endgroup\$
0
\$\begingroup\$

Python 3, 318 301 297 285 272 bytes

Knocked off 17 bytes with the help of DJMcMayhem

Knocked off 4 bytes thanks to mbomb007

Knocked off another 12 bytes thanks to DJMcMayhem

Knocked off another 13 bytes thanks to mbomb007

My first golf ever, so it's not all that great. I used: aliasing math.ceil as y and str.format as z, nesting formats, single line imports, lambda, and bitwise operation plus some other things to get this like it is.

def f(n):import random,math;y=math.ceil;z=str.format;i=y(2*n-5>>1);j=y(n-3>>1);return z("{a}{}{a}",z("{d}(@{}{b}F{c}@)\n{e}"," "*i,b=random.sample(["L","R"],1)[0],c=" "*(2*n-5-i),d=z("(@{}@)\n"," "*(2*n-3))*j,e=z("(@{}@)\n"," "*(2*n-3))*(n-3-j)),a=z("({})\n","@"*(2*n-1)))

Ungolfed version (separated imports, no aliases, and no bitwise operation):

def f(n):
    import random;
    import math;
    return "{a}{}{a}".format(
        "{d}(@{}{b}F{c}@)\n{e}".format(
            " "*(math.ceil((2*n-5)/2)),
            b=random.sample(["L","R"],1)[0],
            c=" "*((2*n)-(5+math.ceil((2*n-5)/2))),
            d="(@{}@)\n".format(" "*(2*n-3))*math.ceil((n-3)/2),
            e="(@{}@)\n".format(" "*(2*n-3))*(n-(3+(math.ceil((n-3)/2))))),
        a="({})\n".format("@"*(2*n-1)))

Interestingly, using the non-bitwise version of this still produces correct output, however, the output is different:

Non-Bitwise:

(@@@@@@@)
(@     @)
(@  LF @)
(@@@@@@@)

Bitwise:

(@@@@@@@)
(@ LF  @)
(@     @)
(@@@@@@@)
\$\endgroup\$
  • \$\begingroup\$ you can move the imports out of the function and change them to from ... import* type imports to save some bytes \$\endgroup\$ – Blue Sep 15 '16 at 16:47
  • \$\begingroup\$ Nice answer, and welcome to the site! A couple tips: 1. lambda functions are shorter, and 2. you could do import math;y=math.ceil to take two bytes off. This would give you import random;import math;y=math.ceil;lambda n:"{a}{}{a}".format("{d}(@{}{b}F{c}@)\n{e}".format(" "*(y((2*n-5)/2)),b=random.sample(["L","R"],1)[0],c=" "*((2*n)-(5+y((2*n-5)/2))),d="(@{}@)\n".format(" "*(2*n-3))*y((n-3)/2),e="(@{}@)\n".format(" "*(2*n-3))*(n-(3+(y((n-3)/2))))),a="({})\n".format("@"*(2*n-1))) which is 13 bytes shorter. \$\endgroup\$ – DJMcMayhem Sep 15 '16 at 16:47
  • \$\begingroup\$ @DJMcMayhem Thanks a lot! I updated my answer. \$\endgroup\$ – L. Steer Sep 15 '16 at 17:03
  • \$\begingroup\$ Glad I could help! One other thing you could probably do is alias to format since you call it a lot. Problem is, I don't exactly know how to alias to a member function (like str.format) so you'd have to experiment a little. I'm not positive it would be shorter though. \$\endgroup\$ – DJMcMayhem Sep 15 '16 at 17:05
  • 4
    \$\begingroup\$ Nice work! I am glad you got something out of this exercise :) However, I would like to clarify that the decoration must be centered to fit the challenge specifications. Whether or not you decide to change your code to reflect this - can I ask that you keep the current code in your post too? Good effort worth keeping, even if it neglects one of the rules. \$\endgroup\$ – Moon Rabbit Sep 16 '16 at 1:28
0
\$\begingroup\$

C# 448 bytes

Golfed:

var v=n%2==0;int l=n+n-3,h=n-2,e=v?0:1,c=e+2;var c2=v?"MFZJLFRFCF":"MAFHMFJCJTTT";var r=new Random().Next(3+e);var f=new String(c2.Skip(c*r).Take(c).ToArray());var mc="";for (var i=0;i < h;i++) {var x="";if (!v && i==((h / 2))) { x=f;} else if (v && ((i==h / 2) || (i==(h/2)-1))) { x +=f[i%2==1?0:1];} var es=x.PadLeft((l/2)+1+e,' ').PadRight(l,' ');mc +="(@"+es+"@)\n";}var b="("+"".PadLeft(l+2, '@')+")";mc=b+"\n"+mc+ b; Console.WriteLine(mc);

Test it here

Ungolfed:

        var v = n % 2 == 0;
        int l = n + n - 3, h = n - 2, e = v ? 0 : 1, c = e + 2;
        var c2 = v ? "MFZJLFRFCF" : "MAFHMFJCJTTT";
        var r = new Random().Next(3 + e);
        var f = new String(c2.Skip(c * r).Take(c).ToArray());
        var mc = "";
        for (var i = 0; i < h; i++)
        {
            var x = "";
            if (!v && i == ((h / 2)))
            {
                x = f;
            }
            else if (v && ((i == h / 2) || (i == (h / 2) - 1)))
            {
                x += f[i % 2 == 1 ? 0 : 1];
            }
            var emptySpace = x.PadLeft((l / 2) + 1 + e, ' ').PadRight(l, ' ');
            mc += "(@" + emptySpace + "@)\n";
        }
        var b = "(" + "".PadLeft(l + 2, '@') + ")";
        mc = b + "\n" + mc + b;
        Console.WriteLine(mc);
\$\endgroup\$

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.