5
\$\begingroup\$

I have a set of colored plastic cups. They come in four colors: green, yellow, pink, and blue. When I put them on my shelf, I like to stack them in a certain pattern. Your job is, given a list of any number of four distinct values (to represent the four cup colors), output a visual representation of how I would stack them.

Rules

  • The cups must be sorted into either two or three stacks.
  • Green and yellow must always be paired with either pink or blue. Which one is paired with which does not matter but must be consistent.
  • If there are an even number of pairs, they must be sorted into two stacks, with each stack having the same number of pair types (unless there are only two pairs, in which case they can be put in any order)
  • The pink and blue cups in a pair must be above the green and yellow ones.
  • Pairs with yellow cups must be above pairs with green cups.
  • Any remaining cups must be evenly distributed amongst the bottoms of the stacks.
  • If there are an even number of pairs and an even number of leftovers, use two stacks.
  • If there are an odd number of ether pairs of leftovers, use three stacks. The extra cups and leftovers must go in the middle stack.
  • Extra cups must be sorted in this order: Yellow above green above blue above pink.

Output

Output must be a representation of the cup layout. Each color must be represented as a consistent one-character value, arranged into columns. A space must be used for empty slots. See below for examples.

Examples

Using GYPB for green, yellow, pink and blue respectively

Input: GGYYPPBB
Output:
BB
YY
PP
GG

Input: GYPB
Output:
PB
GY
or
BP
YG

Input: PPGGBBYYY
Output:
B B
Y Y
P P
GYG

Input: PGPBYBPG
Output:
 B 
 Y 
PBP
GPG
\$\endgroup\$
4
  • 4
    \$\begingroup\$ Could you add the following test cases: GGGYYYPB, GY? (I might be missing something, but I don't see how these could conform to the given rules) \$\endgroup\$ Jun 7 at 15:35
  • \$\begingroup\$ Why do B and Y stand on their own in the last test case? Could you not do something like BB, PP, GG, PY? \$\endgroup\$
    – Ajax1234
    Jun 8 at 15:56
  • \$\begingroup\$ @Ajax1234 Wouldn't that break the rule that the pink and blue cups in a pair must be above the green and yellow ones? \$\endgroup\$
    – Steffan
    Jun 9 at 22:33
  • \$\begingroup\$ @Steffan Perhaps, but in the second and third examples, GY and GYG exist, when "Pairs with yellow cups must be above pairs with green cups." In fact, why could the second example not be BB, YYY, PP, GG? \$\endgroup\$
    – Ajax1234
    Jun 9 at 22:52

0

Browse other questions tagged or ask your own question.