One of the interesting aspects of gravity is that, as far as I'm aware, you can't just have stuff floating in midair.
However, it seems not everyone at the Association of Random Castle Builders is aware of this fact, leading to castles such as this one:
#
#
# # # # ###
#### #### # #
#### # # #### ###
############## ###
###### ###### ###
##### ##### ###
###
``````````````````````````````
and this one:
# # # # # #
##############
### #### ###
# # # # # # # # ### #### ### # # # # # # # #
#### #### #### #### ############## #### #### #### ####
#### # # #### # # #### # # #### ## ######## ## #### # # #### # # #### # # ####
####################################################################################
###### ######## ######## ######## ######## ######## ######## ######## ######
################################### ###### ###################################
################################### ###### ###################################
##
##
##
##
##
````````````````````````````````````````````````````````````````````````````````````````````
and even this one:
##########
#### # ###
#######################
#
#
#
#
# # # #
# # ###
# # ###
# # # # ##
# # ## ###
# # #####
# #####
# # #####
#####
## ##
#####
#####
## ##
## ##
````````````````````````````````````````````
Challenge
For a valid castle, all blocks will be connected to the ground either directly or indirectly. Your program or function will be given a castle such as the ones above as input, and your program must return a truthy or falsy value reflecting whether the castle is valid or not.
Rules
- Input is given as a string.
- All valid castles rest on a surface,
````````
. (If the input string does not contain a surface, the castle is invalid.) - You can assume all input will satisfy these criteria:
- The surface will always be flat.
- The surface will always be at least as wide as the castle, so there will be no blocks to the left or right of the ground.
- The input will never have
#
below the surface. - Input will only contain characters given in this challenge. (
#
,`
, space or newline.) - You may assume the input will always contain at least one character.
- Blocks are connected if they are either horizontally or vertically adjacent. Diagonal does not count!
- Connected:
# or ##
# - Not connected:
# or # # or #
#
#
- Connected:
- Castles must exist to be valid. (In other words, inputs without any
#
must give a falsy value.) - Input will only contain characters given in this challenge. (
#
,`
, space or newline.) - You may assume the input will always contain at least one character.
- Standard I/O and loophole rules apply.
Test cases
Falsy
- All the examples given above.
# # # #
(No ground.)
#### ####
#### # # ####
##############
###### ######
##### ######
(Topmost block is not connected either horizontally or vertically.)
### ####
#### # # ####
##############
###### ######
##### #####
``````````````
```
(Central tower isn't connected to the rest of the castle because there are no horizontally or vertically adjacent blocks connecting it.)
# # # # # #
##############
##### ## #####
# # # # # # # # #### # # #### # # # # # # # #
#### #### #### #### ## #### ## #### #### #### ####
#### # # #### # # #### # # #### # # #### # # #### # # #### # # #### # # ####
####################################################################################
###### ######## ######## ######## ######## ######## ######## ######## ######
################################### ###### ###################################
################################### ###### ###################################
````````````````````````````````````````````````````````````````````````````````````````
(No castle, just a single newline character.)# #
(Rightmost block is not connected either horizontally or vertically.)
#
```````
```
Truthy
#
`# # # #
#### ####
#### # # ####
##############
###### ######
##### #####
``````````````#
#
# # # # ###
#### #### # #
#### # # #### ###
############## ###
###### ###### ###
##### ##### ###
##### ##### ###
``````````````````````````````# # # # # #
##############
### #### ###
# # # # # # # # ### #### ### # # # # # # # #
#### #### #### #### ############## #### #### #### ####
#### # # #### # # #### # # #### ## ######## ## #### # # #### # # #### # # ####
####################################################################################
###### ######## ######## ######## ######## ######## ######## ######## ######
################################### ###### ###################################
################################### ###### ###################################
````````````````````````````````````````````````````````````````````````````````````````````#### ###
# #### ###
# ###
# ##
#
###
#####
#######
#########
##### #####
##### #####
###### ######
#################
# ############# #
#############
#############
#############
###### ######
###### ######
#############
#############
#############
#############
###### ######
###### ######
#############
#############
#############
#############
###### ######
###### ######
#############
#############
#############
#############
#############
##### #####
##### #####
##### #####
`````````````````````
####
#####
######
####
####
#####
########
##########
##########
###########
############
##############
##### ################
########### #################
###########################################
########################################
#####################################
##################################
############################
###################
``````````````````````````````````````````````````
Good luck!