40
2
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!
Can we assume all lines of the input will be the same length (i.e. filled up with spaces)? – smls – 2017-02-03T13:03:55.410
@smls No, you cannot assume input will be padded. – user2428118 – 2017-02-03T13:08:23.943
Some more test-cases that might be good to add: 1) Non-flat surface. 2) Too short surface. 3) Castle not connected to the surface. 4) Block connected to nothing but a surface. – smls – 2017-02-03T13:35:43.447
1@smls Re #1 and #2: I actually meant to specify that submissions don't have to handle that, but I now see that's not how I wrote it down. Since there are no solutions posted yet that handle these things, I'll update the question so that it's clear you don't have to handle these. Re #3: I can't really think of a situation where code would correctly handle Falsy test case 2, 4 and 6 and yet fail to detect a situation where there are no blocks at all connected to the ground. Re #4: I'm not sure what you mean by that. Isn't that already handled by Truthy test case number 1? – user2428118 – 2017-02-03T13:46:19.917
1Related. – Zgarb – 2017-02-03T14:10:56.577
@user2428118: You're right, they're already implicitly handled. – smls – 2017-02-03T14:43:23.260
How about an input where the castle is connected to the ground from the bottom (falsy I assume). – feersum – 2017-02-03T15:35:06.117
@feersum I hadn't thought of that yet... I don't want to render the existing answers invalid, so I'll specify that case doesn't have to be handled. – user2428118 – 2017-02-03T16:09:42.343
@user2428118 When you say input cannot be padded, do you mean right-padded, or on both sides? It seems like the string would have to be left-padd to align properly, but maybe I don't understand the input format. – Brian J – 2017-02-03T20:30:41.463
@Brian You've misread my comment. I said "you cannot assume input will be padded", in other words: input may or may not have padding and your code should work regardless of whether the input is padded or not. – user2428118 – 2017-02-03T21:03:57.650
@user2428118 So in the banana example, what would the input string look like? How do we know that the stem is on the right side of the banana? – Brian J – 2017-02-03T21:09:58.977
@BrianJ The original comment by smls asked if submissions can assume that all lines are of equal length, so by adding extra spaces to the right to make sure no line is shorter or longer than the others. Which I choose not to allow. Anyway, here is the banana with newlines replaced by
– user2428118 – 2017-02-03T21:25:54.687\n
. I hope it's clear now?@user2428118 That pastebin makes it very clear to me. I was trying to ask if it would look like that when I said left-padding. Sorry for choosing confusing terminology. – Brian J – 2017-02-03T21:33:24.620
2A banana castle? BEST CASTLE EVER – Matthew Roh – 2017-02-04T16:32:03.270
Now I need to write an AHK script that opens MSPaint and uses the Paintcan tool. – corsiKa – 2017-02-05T04:29:08.853
As usual, left-pad causes trouble. – David Conrad – 2017-02-06T18:54:18.933