45
\$\begingroup\$

Programming Puzzles & Code Golf is about to get a new moderator, Dennis! This challenge is a tribute to him and our other active (or recently active) moderators: Doorknob, Martin Büttner, and Chris Jester-Young. The challenge title is meant to be read to the tune of the Pepto Bismol song.

Basically, we're going to treat them all to pizza at The Nineteenth Bite Pizzeria, but we need to make sure they share it fairly because some of the mods are known to be pizza addicts!

The pizzas sold by the pizzeria are all rectangular blocks of text. The width and length of a pizza may be any non-negative integers as long as their product is divisible by four. Each grid space in the block of text pizza represents a slice, so it's always possible to split the slices up into four equal groups.

The mods will collectively order a single pizza, providing its width and length parameters to their server in any reasonable format such as [width],[length]. Just before the pizza arrives at their table, you need to label each slice with the initial of the mod who gets to eat it to ensure they all share fairly. Everyone should get the same number of slices.

  • E is for Dennis
  • D is for Doorknob
  • M is for Martin
  • C is for Chris

The mods are a little persnickety, however, and require that their respective sets of slices be path-connected, that is, that all their slices can be reached from one another by moving up, down, left, and right, not crossing anyone else's slices (and not moving diagonal). The don't care how you do this as long as it is done.

Once you've accurately labeled each slice, deliver the pizza to the mods with an optional trailing newline.

Your labeler may be a program or a function and can print or return the labeled pizza. The shortest labeler in bites wins.

Examples

Example 1

Order: 4,1

Some Possible Labeled Pizzas:

EDMC
MEDC
CDEM

Example 2

Order: 4,4

Some Possible Labeled Pizzas:

MMMM
CCCC
DDDD
EEEE
DEMC
DEMC
DEMC
DEMC
CCCC
DEEM
DEEM
DDMM

Example 3

Order: 8,3

Some Possible Labeled Pizzas:

MMMMMMCC
DDDDCCCC
DDEEEEEE
DDDCMMMM
DCDCMEEE
DCCCMEEE

Example 4

Order: 20,5

A Possible Labeled Pizza:

DDDDDDDDDDDDMCCCCCCCCCCC
DEEEEEEEEEEDMMMMMMMCCCCC
DEEEEEEEEEEDMMMCCCCCCCCC
DEEEEEEEEEEDMMMMMMMMCCCC
DDDDDDDDDDDDMMMMMMMMMMMC

(The D's here are not simply-connected but that's ok.)

\$\endgroup\$
13
  • 12
    \$\begingroup\$ @BetaDecay It's mainly a diarrhea rhyming device. \$\endgroup\$ Commented Aug 28, 2015 at 20:21
  • 30
    \$\begingroup\$ They require that their respective sets of slices be path-connected. It's a little scary that you'd know that about me... \$\endgroup\$
    – Dennis
    Commented Aug 28, 2015 at 20:36
  • 23
    \$\begingroup\$ Okay, what is with this weird rectangular-outline-shaped pizza slice you've given me? I want a refund! \$\endgroup\$
    – Doorknob
    Commented Aug 28, 2015 at 20:44
  • 12
    \$\begingroup\$ @flawr Not for the likes of you, non-mod. \$\endgroup\$ Commented Aug 28, 2015 at 23:52
  • 14
    \$\begingroup\$ "Shortest labeller in bites wins" - I see what you did there. \$\endgroup\$
    – DankMemes
    Commented Aug 29, 2015 at 0:07

9 Answers 9

22
\$\begingroup\$

CJam, 20 bytes

q~1$*4/"CEDM"e*/:$N*

I think this should work :)

Try it online

Explanation:

This first makes a pizza labeled CC…EE…DD…MM… from left to right and top to bottom, then sorts each row in alphabetical order. The only disconnections can happen between the C-E border and E-D border, or E-D border and D-M border (if they fall on adjacent rows). But the sorting ensures that the E's go to the right side and the D's go to the left side, as C<E>D<M, so the E's and the D's remain connected.

q~          read and evaluate the input
1$          copy the width
*4/         multiply with the height and divide by 4
"CEDM"e*    repeat each letter in "CEDM" that many times
/           split into rows of the given width
:$          sort each row
N*          join with newlines
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Selectively reversing rows shared by Doorknob and me. That's one clever use of $! \$\endgroup\$
    – Dennis
    Commented Aug 28, 2015 at 22:47
7
\$\begingroup\$

K, 61 bytes

{[a;b]{((+:;|:)@x!2)[r@x]}'!#r:(b;a)#,/{(_(a*b)%4)#x}'"EDMC"}

Examples:

ryan@DevPC-LX:~/golf$ rlwrap k2
K 2.8 2000-10-10 Copyright (C) 1993-2000 Kx Systems
Evaluation. Not for commercial use. 
\ for help. \\ to exit.

  `0:{[a;b]{((+:;|:)@x!2)[r@x]}'!#r:(b;a)#,/{(_(a*b)%4)#x}'"EDMC"}[4;1]
EDMC
  `0:{[a;b]{((+:;|:)@x!2)[r@x]}'!#r:(b;a)#,/{(_(a*b)%4)#x}'"EDMC"}[4;4]
EEEE
DDDD
MMMM
CCCC
  `0:{[a;b]{((+:;|:)@x!2)[r@x]}'!#r:(b;a)#,/{(_(a*b)%4)#x}'"EDMC"}[8;3]
EEEEEEDD
MMMMDDDD
MMCCCCCC
  `0:{[a;b]{((+:;|:)@x!2)[r@x]}'!#r:(b;a)#,/{(_(a*b)%4)#x}'"EDMC"}[20;5]
EEEEEEEEEEEEEEEEEEEE
DDDDDDDDDDDDDDDEEEEE
DDDDDDDDDDMMMMMMMMMM
CCCCCMMMMMMMMMMMMMMM
CCCCCCCCCCCCCCCCCCCC
  
ryan@DevPC-LX:~/golf$ 

I would hate to be the person who has to slice these things...

\$\endgroup\$
4
  • \$\begingroup\$ Looks like it zig-zags back and forth, top to bottom. Is that correct? (Will it work for 1,8?) \$\endgroup\$ Commented Aug 28, 2015 at 20:47
  • \$\begingroup\$ @Calvin'sHobbies It appears to work for that input. \$\endgroup\$ Commented Aug 28, 2015 at 20:48
  • 1
    \$\begingroup\$ I was going to use the same approach and I'm pretty sure it works. It's Chris though. \$\endgroup\$
    – Dennis
    Commented Aug 28, 2015 at 20:50
  • \$\begingroup\$ @Dennis Whoops. Fixed. \$\endgroup\$ Commented Aug 28, 2015 at 21:06
7
\$\begingroup\$

Pyth, 30 25 bytes

jb.eu_GkbceQs*R/*FQ4"EDMC

Live demo and all test cases.

Cut off 5 bytes thanks to @Jakube!

Same algorithm as my K answer...but a lot shorter.

30-byte version:

jb.e?%k2_bbcjkm*/*FQ4d"EDMC"hQ

Live demo and all test cases for 50-byte version.

\$\endgroup\$
0
7
\$\begingroup\$

Pyth, 20 bytes

VceQs*L/*FQ4"CEDM"SN

Uses @aditsu's sorting trick.

Demonstration.

I came up with a large number of alternative programs of the same length while trying to golf this:

VceQs*L/*FQ4"CEDM"SN
VceQsCm"CEDM"/*FQ4SN
VceQs*L*FQ"CEDM"%4SN
VceQ%4s*L*FQ"CEDM"SN
VcQs*L/*Qvz4"CEDM"SN    (Newline separated input)
jbSMceQs*L/*FQ4"CEDM
Vcs*L/*FQ4"CEDM"hQSN
\$\endgroup\$
6
\$\begingroup\$

Stuck, 42 33

It returns! And in awfully long form. :( -- I stole aditsu's sort idea to save 9 bytes :)

t;g*4/[4*"CEDM"z"];F*":j0GK'$:Nj

Explanation:

t;g*4/                              # Take |-separated input,
                                    #   store the first value in var stack,
                                    #   multiply the two numbers and divide by 4.
      [4*"CEDM"z                    # Wrap that value in an array, make 3 copies
                                    #   to get a list of length 4, zip with "EDMC"
                "];F*":j0GK         # For each item, multiply letter by value to
                                    #   to get string, join the result, split into
                                    #   segments the size of the saved variable.
                            '$:Nj   # For each item, sort so the letters are in the correct
                                    #   order, join by newline, print.

Example input:

20|5

Example output:

CCCCCCCCCCCCCCCCCCCC
CCCCCEEEEEEEEEEEEEEE
DDDDDDDDDDEEEEEEEEEE
DDDDDDDDDDDDDDDMMMMM
MMMMMMMMMMMMMMMMMMMM
\$\endgroup\$
3
  • \$\begingroup\$ Are you sure it will be simply connected for 8|3? \$\endgroup\$
    – yo'
    Commented Aug 31, 2015 at 10:04
  • \$\begingroup\$ @yo' Yes. It outputs CCCCCCEE\nDDDDEEEE\nDDMMMMMM. \$\endgroup\$
    – Kade
    Commented Aug 31, 2015 at 13:54
  • \$\begingroup\$ /me_stupid, sorry for that. \$\endgroup\$
    – yo'
    Commented Aug 31, 2015 at 19:13
4
\$\begingroup\$

Rev 1 C, 74

i;f(w,h){for(i=w*h;i--;i%w||puts(""))putchar(h-i/w*2==1^"CDEM"[i*4/w/h]);}

For a 1-byte saving, this version reverses (only) the middle row of slices for any odd number of rows.

Rev 0 C,75

i;f(w,h){for(i=w*h;i--;i%w||puts(""))putchar(h==3&i/w==1^"CDEM"[i*4/w/h]);}

Many answers here zigzag, but in most cases, just outputting the letters in order (left to right, top to bottom) works fine:

No need to zigzag for heights 1,2 or 4

No need to zigzag for heights greater than 4 (each mod's pizza ration will wrap around.)

Therefore we only actually need to zigzag when the height is 3, and then we only need to reverse the middle row.

As it turns out, Dennis and Doorknob are the only mods on that row. And they can be interchanged by XORing their ASCII codes with 1.

This is handy given that there's no easy way to reverse a string in C.

Ungolfed in test program

i;
f(w,h){
  for(i=w*h;i--;i%w||puts(""))            //loop through all squares. puts inserts a newline at the END of each line.
  putchar(h==3&i/w==1^"CDEM"[i*4/w/h]);   //put the letter, XORing D and E for the middle row of height 3.
}

W,H;
main(){
  scanf("%d%d",&W,&H);
  f(W,H);
} 
\$\endgroup\$
1
1
\$\begingroup\$

JavaScript (ES6) 107

Zigzag solution. Using template string, the newline is significant and counted.

Test running the snippet with FireFox.

f=(w,h)=>{for(i=o=r='',z=l=1;--l?l:c='CDEM'[l=w*h/4,i++];r[w-1]&&(o+=r+`
`,r='',z=!z))r=z?r+c:c+r;return o}


//TEST

(test=_=>([w,h]=I.value.match(/\d+/g),O.innerHTML=f(w,h)))()
<input id=I value='8 3'><button onclick='test()'>-></button>
<pre id=O></pre>

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

Retina, 83 bytes

The features used in this answer are newer than this challenge (not that it matters...). Byte count assumes ISO 8859-1 encoding.

\d+
$*#
#(?=.*¶(.+))
$1
\G####
CDEM
S_`((.)+?(?=.*¶(?<-2>#)+$))|\D
O`.
T`DE`ED
O%`.

Try it online!

This implements aditsu's solution which is now somewhat feasible thanks to the new sort stages.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 14 bytes

×:4“CEDM”xsṢ€Y

Try it online!

Implements aditsu's algorithm

How it works

×:4“CEDM”xsṢ€Y - Main link. Takes h on the left and w on the right
×              - Yield h×w
 :4            - Integer divide by 4; Yield R = (h×w):4
   “CEDM”      - Yield "CEDM"
         x     - Repeat each character R times
          s    - Split into rows of length w
           Ṣ€  - Sort each row
             Y - Join by newlines
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.