Dennis, Doorknob, Martin Büttner, Chris Jester-Young - Pizzeria!

42

3

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.)

Calvin's Hobbies

Posted 2015-08-28T20:06:36.430

Reputation: 84 000

I'd love to see what goes on in your mind :D – Beta Decay – 2015-08-28T20:18:02.290

12@BetaDecay It's mainly a diarrhea rhyming device. – Calvin's Hobbies – 2015-08-28T20:21:58.267

3That is not what I imagined... D: – Beta Decay – 2015-08-28T20:22:59.947

29They require that their respective sets of slices be path-connected. It's a little scary that you'd know that about me... – Dennis – 2015-08-28T20:36:43.420

22Okay, what is with this weird rectangular-outline-shaped pizza slice you've given me? I want a refund! – Doorknob – 2015-08-28T20:44:20.790

Is there pineapple pizza available? – flawr – 2015-08-28T23:43:15.937

12@flawr Not for the likes of you, non-mod. – Calvin's Hobbies – 2015-08-28T23:52:54.560

14"Shortest labeller in bites wins" - I see what you did there. – DankMemes – 2015-08-29T00:07:15.607

Are we allowed to transpose the pizza slices? Like, if the request is for a width of 5 and a height of 4, can we have a width of 4 and a height of 5? – Nzall – 2015-08-30T21:58:37.517

@NateKerkhofs No – Calvin's Hobbies – 2015-08-30T22:25:01.597

4Oh, no! There isn't enough pizza for Alex D: – Beta Decay – 2015-08-31T16:27:53.363

What if you made it a requirement that the path couldn't repeat itself? – TheDoctor – 2015-09-02T01:57:07.273

Answers

21

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

aditsu quit because SE is EVIL

Posted 2015-08-28T20:06:36.430

Reputation: 22 326

1Selectively reversing rows shared by Doorknob and me. That's one clever use of $! – Dennis – 2015-08-28T22:47:38.027

8

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.

kirbyfan64sos

Posted 2015-08-28T20:06:36.430

Reputation: 8 730

7

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...

kirbyfan64sos

Posted 2015-08-28T20:06:36.430

Reputation: 8 730

Looks like it zig-zags back and forth, top to bottom. Is that correct? (Will it work for 1,8?) – Calvin's Hobbies – 2015-08-28T20:47:44.560

@Calvin'sHobbies It appears to work for that input. – kirbyfan64sos – 2015-08-28T20:48:30.010

1I was going to use the same approach and I'm pretty sure it works. It's Chris though. – Dennis – 2015-08-28T20:50:22.063

@Dennis Whoops. Fixed. – kirbyfan64sos – 2015-08-28T21:06:14.767

7

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

isaacg

Posted 2015-08-28T20:06:36.430

Reputation: 39 268

6

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

Kade

Posted 2015-08-28T20:06:36.430

Reputation: 7 463

Are you sure it will be simply connected for 8|3? – yo' – 2015-08-31T10:04:02.923

@yo' Yes. It outputs CCCCCCEE\nDDDDEEEE\nDDMMMMMM. – Kade – 2015-08-31T13:54:27.810

/me_stupid, sorry for that. – yo' – 2015-08-31T19:13:55.117

4

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);
} 

Level River St

Posted 2015-08-28T20:06:36.430

Reputation: 22 049

Great explanation. – trichoplax – 2015-09-05T12:55:15.207

1

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>

edc65

Posted 2015-08-28T20:06:36.430

Reputation: 31 086

0

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.

Martin Ender

Posted 2015-08-28T20:06:36.430

Reputation: 184 808