58
8
Your challenge is given an input of a prison layout to work out whether any of the prisoners can escape.
Input
Input may be in any reasonable format such as a string, array, array of arrays etc. The input will consist of three characters, in this case #
, P
and space. The input will not necessarily contain all three characters.
#
: A wallP
: A prisoner- space: An empty space
An example input will look like:
#####
# #
# P #
# #
#####
Output
A truthy/falsey value of whether or not the prison is secure. The prison is only secure if it can hold all of the prisoners. If any prisoner can escape it is not secure.
A prisoner can escape if they are not fully enclosed by a wall. A diagonal joining is fully enclosed.
Test cases
############# Truthy
# P # P# #
# # # P #
#############
############# Truthy
# P P #
# # # P #
#############
############# Falsey
# P # P# #
# # # P #
########## ##
#### Truthy
# #
# #
# P ####
####
P Falsey
### Falsey
# #
# #
### P
8I have a feeling this is a duplicate or at least a similar challenge. Good challenge anyways. – John Dvorak – 2017-06-14T12:53:45.543
May we require that the input be padded to a rectangle? – John Dvorak – 2017-06-14T12:54:29.297
1@JanDvorak You can have leading and trailing whitespace yes. – TheLethalCoder – 2017-06-14T12:55:21.470
2@JanDvorak It might be but with my limited Google Fu I could not find a duplicate. – TheLethalCoder – 2017-06-14T12:55:54.260
@JanDvorak It also looks very familiar to me. I think I've seen a similar challenge in the Sandbox before, iirc. – Kevin Cruijssen – 2017-06-14T13:26:33.977
Could you add a test case where we have a closed box, but with a prisoner outside it? I.e.
####\n# #\n# #\n#### P
– Kevin Cruijssen – 2017-06-14T13:31:14.603@KevinCruijssen Added – TheLethalCoder – 2017-06-14T13:32:48.357
@JanDvorak Aren't you thinking about the challenge where you had to plan the layout of the prison? Can't find it anymore but I remember it well. – LiefdeWen – 2017-06-14T14:10:49.177
I would personally move the prisoner in the third test case one place to their right so that they no longer have a direct vertical path to freedom. – Jonathan Allan – 2017-06-14T14:15:26.307
1
Somehow related
– officialaimm – 2017-06-14T16:09:14.2202related (Flood-fill a 2D grid) – Esolanging Fruit – 2017-06-14T16:32:23.103
3It would be good to have Falsey examples where both horizontal and vertical movement are required to escape. – xnor – 2017-06-14T20:38:44.943
1I wonder if there's any convenient way to interpret this input as a bitmap and use an image editor's flood fill tool on it. – user2357112 supports Monica – 2017-06-15T23:00:40.540
Possible duplicate of Thinking outside the box - Am I doing it right?
– FantaC – 2018-03-02T00:19:14.2432@tfbninja Not really a duplicate. That one asks to try to have the program extrapolate from given data to determine if the word is in the box. This one is BFS floodfill to see if there are unenclosed spaces holding marked values. – HyperNeutrino – 2018-03-02T02:57:23.253
1
@tfbninja Certainly not a duplicate. The other question also restrict the wrapper to be in the shape of a box, and only give partial information. Still, useful as related.
– user202729 – 2018-03-02T06:07:56.843@HyperNeutrino and user202729 I see your point, I will remove my close vote – FantaC – 2018-03-02T16:13:06.413