30
4
In the ASCII art world, there is water, hash walls and letter mechanisms.
You are in a room made up of hash walls (#
signs):
#######
# #
# #
# #
# ### #
# #
#######
You install an S water source (S
sign) and an E water tank (E
sign) which can receive water from any direction, but you only have one S source and one E tank.
#######
# S #
# #
# #
# ### #
# E #
#######
So you have to select wisely where to place the source. That's where you pull off your code-golf skills.
The task
You get an input consisting of a string representing a room with the source and the tank:
#######
# S #
# #
# #
# ### #
# E #
#######
You have to find out if the water ultimately reaches the tank. The water flows down, if possible, else to the left and right, if possible. The water doesn't accumulate because it doesn't go up.
So, for the above input, the result is:
#######
# * #
# * #
#*****#
#*###*#
#**O**#
#######
The water happily reaches the tank, so you must output a truthy value.
But if the water doesn't reach the tank:
#######
#S #
# #
# E #
# ### #
# #
#######
#######
#* #
#* #
#* X #
#*### #
#*****#
#######
Then you must output a falsy value.
Write a program to decide whether the water ultimately reaches the tank. Your code should be as short as possible.
Assumptions
Assume that the input is always valid (the entire room is an enclosed rectangular region with the S and E).
Assume there is only one room provided as input.
Test Cases
Your program should return a truthy value for the following test cases:
#######
# S #
# #
# #
# ### #
# E #
#######
#######
# S #
# #
# E #
# #
# #
#######
#######
# #
# #
# SE #
# ### #
# #
#######
###############################################
# S #
# #
# #
# #
# ############### #
# #
# ################## ################## #
# #
# #
# ##### #
# E #
###############################################
#######
# S #
# #
# #
# ### #
# # #
### ###
## E ##
# #
#######
But a falsy value for the following test cases:
#######
#S #
# #
# E #
# ### #
# #
#######
#######
# #
# SE #
# #
# #
# #
#######
#######
# #
# E #
# #
# S #
# #
#######
####################################
# #
# #
# #
#S # E#
####################################
The second to last room in the True category and the last room in the False category were shamelessly stolen borrowed from Koth: Jump and Run by Manu (who deleted the sandbox post).
The last room in the True category is from Martin Buttner's answer in Retina.
Note: I deleted my KOTH sandbox post, your challenge looks much better :) – CommonGuy – 2016-02-29T10:33:14.013
Does'nt the water accumulated until it fill any room? Thus the water always reach the tank if and only if they are in the same room. – Bob – 2016-02-29T10:50:34.123
@Bob no, the water doesn't accumulate. It doesn't go up. Also, there is only one room provided as input. – user48538 – 2016-02-29T10:52:58.863
1Pro tip for formatting test cases in true/false challenges (or classification challenges with few classes): group the test cases by output and separate the groups so you can avoid the
from
/to
/really bits (which makes it easier for participants to process all test cases at once). – Martin Ender – 2016-02-29T12:36:55.007I'll incrementally move the green tick to golfier submissions as they win against the reigning accepted answer. – user48538 – 2016-02-29T14:22:39.783
Note that while using that way, I still encourage more answers into the question. The accepting of an answer is not the end of the challenge. – user48538 – 2016-02-29T17:55:47.380
1So basically Minecraft liquid flow logic. Although in Minecraft I think the 3rd in your true test cases would return false since the water would only go towards the left side. – Patrick Roberts – 2016-02-29T19:22:49.233
But the tank is not a solid block. It is a block which receives water from every direction. – user48538 – 2016-02-29T19:43:43.637
1Reminds me of falling-sand water physics. – user253751 – 2016-02-29T20:06:53.713
Are there any restrictions as to how the input should be taken in? Strings, array...? – ricdesi – 2016-02-29T20:47:23.970
The whole input is a string. – user48538 – 2016-02-29T20:49:04.583
I wonder how much longer the code would be to show the flow of the water so you can see where it all ends up – gabe3886 – 2016-03-01T11:22:45.883