Will it ​float?

10

1

The challenge

Given a 2d string representing the bottom of a boat as an input, you must determine whether or not the boat will float. This 2D string can be in whatever format is most convenient. (String with newlines, list of string, list of list of chars, etc.) Print a truthy value if it will float, and a falsey value if it will sink.

A boat will tip over if the bottom has an inconsistent density, so every character of must be the same. Also, if a boat has large holes in it, represented by spaces, it will sink, so your boat must have no holes with an area greater than 4. Here is an example:

########
#   ####
########
#  ## ##
#  #####
########

This boat is valid because the largest hole in it has an area of 4. This boat:

########
########
#     ##
#  #####
########

is invalid because it has a hole with an area of 7. You can safely assume that the outside of every input will be a solid rectangle with no holes. Here are some more tests:

$$$$$$$$
***$$$$$
***$$$$$
***$$$$$
$$$$$$$$
Invalid density. Sink.

%%%%%%%%
%    % %
%%%%%  %
%    % %
%%%%%%%%
None of the holes are larger than 4. Float.

OOOOOOOO
OOOOOOO 
OOOOOOOO
OOOOOOOO
OOOOOOOO
The outside border is not solid. Undefined.

&&&&&&&&&&&&&
& & & & & & &
&& & & & & &&
& & & & & & &
&& & & & & &&
& & & & & & &
&&&&&&&&&&&&&
Although I would not be comfortable riding in this boat myself, 
none of the holes are larger than 4. It floats.

@@@@@
@   @
@   @
@   @
@@@@@
It sinks.

Rules

  • IO can be in any reasonable format.
  • Standard loopholes apply.
  • Shortest answer in bytes wins.
  • The given string will entirely consist of printable ASCII.

James

Posted 2016-04-30T20:04:26.657

Reputation: 54 537

What is the bottom? What is the frame? – flawr – 2016-04-30T20:10:42.307

@flawr The bottom is the string you take as input. The frame is a poor choice of wording that I will edit out. – James – 2016-04-30T20:12:41.023

A "2d string"? You mean a list of strings? – Fund Monica's Lawsuit – 2016-04-30T20:14:00.683

I think the & boat exists – l4m2 – 2018-06-16T04:52:24.440

"A boat will tip over if the bottom has an inconsistent density, so every character of must be the same." If the outside border was one character and the interior were a different character it would be inconsistent density but it wouldn't tip over would it? – Jerry Jeremiah – 2018-06-18T03:18:42.300

I would like to see an answer using PMA/Snails... – Jerry Jeremiah – 2018-06-18T03:21:03.007

Answers

3

Matlab, 106 bytes

s=input('');im=~(s-32);c=bwconncomp(im,4);disp(~nnz(cellfun(@nnz,c.PixelIdxList)>3)&nnz(unique(s(~im)))<2)

The input is a matrix of characters, e.g. for the first test case:

`['$$$$$$$$';'***$$$$$';'***$$$$$';'***$$$$$';'$$$$$$$$']`

Explanation:

s=input('');           %read input
im=~(s-32);            %convert input to bw image (space = black)
c=bwconncomp(im,4);    %calculate the connected components (4 connectivity)

disp(
     ~nnz(cellfun(@nnz,c.PixelIdxList)>3) %find out whether we have components that have more at least 4 pixels
     &nnz(unique(s(~im)))<2)              %find out if we have more than 1 non-space character

flawr

Posted 2016-04-30T20:04:26.657

Reputation: 40 560