3
\$\begingroup\$

There is a parade marching through a city! There are 3 main groups of marchers: the (B)and, Poster (C)arriers, and (F)lag Holders. Also, every (P)oliceman in the whole city is on duty.

Flag holders (F) can march anywhere in the parade, but two flag holders cannot march next to each other, unless three or more flag holders are together.

The band (B) has to be together. The poster carriers (C) all have to be behind the band (either together, or in two groups).

Given the number of flag holders, poster carriers, band members, and policemen separated by your choice of delimiter, print one line for every possible configuration, organized in alphabetical order.

Test case 1:

Input:
2 2 2 0
Output:
CCFBBF
CFCBBF
CFCFBB
FCCBBF
FCCFBB
FCFCBB

Test case 2:

Input:
3 3 3 0
Output:
CCCBBBFFF
CCCFFFBBB
CCFCFBBBF
CCFFFCBBB
CFCCFBBBF
CFCFCBBBF
CFFFCCBBB
FCCCFBBBF
FCCFCBBBF
FCFCCBBBF
FCFCFCBBB
FFFCCCBBB

Test case 3:

Input:
2 2 2 1
Output:
CCBBFPF
CCFBBFP
CCFBBPF
CCFPBBF
CCFPFBB
CCPFBBF
CFCBBFP
CFCBBPF
CFCFBBP
CFCFPBB
CFCPBBF
CFCPFBB
CFPCBBF
CFPCFBB
CFPFCBB
CPCFBBF
CPFCBBF
CPFCFBB
FCCBBFP
FCCBBPF
FCCFBBP
FCCFPBB
FCCPBBF
FCCPFBB
FCFCBBP
FCFCPBB
FCFPCBB
FCPCBBF
FCPCFBB
FCPFCBB
FPCCBBF
FPCCFBB
FPCFCBB
FPFCCBB
PCCFBBF
PCFCBBF
PCFCFBB
PFCCBBF
PFCCFBB
PFCFCBB

This is code-golf, so the shortest source code in bytes wins.
*Note: It doesn't count if it takes longer than 3 seconds to run each test case. I won't use integers more than 20.

\$\endgroup\$
  • 2
    \$\begingroup\$ What part is in alphabetical order? \$\endgroup\$ – atlasologist Jul 1 '16 at 12:31
  • \$\begingroup\$ I've reopened it, but at least your first example still isn't in alphabetical order. \$\endgroup\$ – Martin Ender Jul 2 '16 at 10:17
  • \$\begingroup\$ @atlasologist the lines are sorted in alphabetical order \$\endgroup\$ – thepiercingarrow Jul 3 '16 at 0:46
  • \$\begingroup\$ I might be misunderstanding the conditions in the question, but for the first example, why is CFCBBF not a valid arrangement? \$\endgroup\$ – 2012ssohn Jul 3 '16 at 3:14
  • \$\begingroup\$ @2012ssohn whoops, it is. It probably got deleted by accident during my test-case rewrite, sorry about that. \$\endgroup\$ – thepiercingarrow Jul 3 '16 at 6:26
2
\$\begingroup\$

CJam, 49 bytes

l~"BCFP".*(a\s+m!:s{_"FP"-W%_$=\e`[2'F]e=!&},_&N*

Try it online!

Input format is [B C F P].

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Pyth, 56 bytes

jf&!},2\FrT8!:T"B.*C|C([^C]+C+){2}|B[^B]+B"0{S.ps*V"FCBP

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Could be golfed much further, but just post another answer if you are using Pyth. \$\endgroup\$ – Leaky Nun Jul 1 '16 at 10:25
  • \$\begingroup\$ I changed specs, but since you submitted before I changed, you don't have to redo your submission - this is valid :) \$\endgroup\$ – thepiercingarrow Jul 2 '16 at 7:29
  • \$\begingroup\$ Well of course it would be easier if I can just obtain the number of police directly. \$\endgroup\$ – Leaky Nun Jul 2 '16 at 9:38

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.