15
3
There's a river and there are wolves and chickens on one side of the river. They have a raft and they all need to get to the other side. However, the raft cannot travel on its own. The raft will sink if more than two animals are on it. None of the animals want to get wet because the river's cold and dirty. None of the animals can jump or fly over the river. Also, if there are chickens on one side, there cannot be more wolves on that side than there are chickens on that side -- the wolves will then decide to eat the chickens. This means that you cannot take two wolves on the raft to a side with one chicken.
Your task is to make a program/function that takes a number of wolves and a number of chickens (greater than or equal to the number of wolves) as input and finds the smallest number of times the raft has to move across the river. If the task is not possible, the program/function should output/return an empty string. It will then print/return one method as to how this is done in the following way:
W if a wolf crosses the river on its own
C if a chicken crosses the river on its own
CW if a chicken and a wolf cross the river -- WC is also fine
CC if two chickens cross the river
WW if two wolves cross the river
As you can deduce, the raft will automatically move in alternating directions (left and right, starting from left to right as the first one or two animals cross the river). This doesn't need to be outputted/returned. 'W', 'C', 'CW', 'CC' or 'WW' in the output may be separated by at least one of the following:
spaces (' ')
commas (',')
newlines
Alternatively, you may store the directions as items in a list (an empty list means no solution).
Test cases (output separated by commas -- input takes the form wolves,chickens
):
1,1 -> CW
2,2 -> CW,C,CC,C,CW
1,2 -> CW,W,CW
0,10 -> CC,C,CC,C,CC,C,CC,C,CC,C,CC,C,CC,C,CC,C,CC
3,2 -> no solution
Try to make your code as short in bytes as possible.
The solution for (3,2)? – Magic Octopus Urn – 2016-10-20T19:47:21.310
@carusocomputing It doesn't work, because there are more wolves than chickens. So there's no solution. – 0WJYxW9FMN – 2016-10-20T19:48:35.767
Ahh... Maybe label the inputs as W=3, C=2 or something; was a tad confusing to process, other than that this looks cool. – Magic Octopus Urn – 2016-10-20T19:50:55.880
@carusocomputing I would, but I think that it would be more confusing because the input is 3,2 and not W=3,C=2. – 0WJYxW9FMN – 2016-10-20T19:52:13.453
For the 2,2 test case, is this solution acceptable ( CW,C,CW,W,CW) ? I think this is the only way to support solutions to equal counts higher than 2. – None – 2016-10-20T23:10:27.803
1
Hoping for a solution in chicken
– Robert Fraser – 2016-10-21T06:14:50.220Is the
permutations
tag really suitable here? – Arnauld – 2016-10-21T14:27:07.013@Arnauld Well, the challenge might use permutations... – 0WJYxW9FMN – 2016-10-21T16:07:48.043
@Phaeze I haven't tried programming this -- it was a problem in the maths club at my school. I think you're right, but just in case, I won't add it in. – 0WJYxW9FMN – 2016-10-21T16:07:56.170
@RobertFraser Nice idea! I rather like the idea of that language -- more interesting to golf in than brainf*** because it's slightly less low-level. – 0WJYxW9FMN – 2016-10-21T16:10:38.153
Is a leading space allowed? e.g.
" CW C CW"
? – MildlyMilquetoast – 2017-01-20T05:01:09.233@MistahFiggins No. – 0WJYxW9FMN – 2017-01-20T21:19:35.487
Those are some powerful chickens if they can take on the wolves one on one :) – aditsu quit because SE is EVIL – 2017-01-21T08:58:04.390
"takes a number of wolves and a number of chickens (greater than or equal to the number of wolves)" -- Then why does the last test-case exist? Do we have to deal with the case of wolves>chickens, or not? – smls – 2017-01-21T16:59:03.580