You probably know the rhetorical question of whether a glass is half full or half empty. I'm getting a little tired of the phrase, so I decided that it's time to eliminate this confusion about glass fullness or emptiness programmatically.
Your task is to write a program that takes an ASCII art representation of an ugly glass and outputs an ASCII art of a corresponding nice glass. It also has to decide whether the glass is full
, mostly full
, mostly empty
or empty
and output this as well (any 4 constant, distinct output values do).
TL;DR
Input is an ASCII art of a glass (#
characters) and liquid (a-z
) distributed randomly inside and outside of the glass. Liquid within the glass falls down and accumulates at its bottom, liquid outside of it gets discarded. Output an ASCII art of the glass after the liquid has settled at the bottom. Determine how full the glass is and output that as well.
Ugly and nice glasses
A glass in general is a container made out of #
characters with a bottom, two side walls and no top.
- Valid glasses do not have holes in them. (All of the
#
characters have to be connected.) - There will either be at least two
#
characters in each line of the input ASCII art, or none. There won't be a line with exactly one#
. - The top line of the input ASCII art will always have exactly two
#
. - Valid glasses have exactly one local minimum in their delimiting wall of
#
characters. This means that liquid can't get trapped somewhere. - The delimiting wall of a glass will not have local maxima.
- There won't be any
#
below the bottom of the glass. - The interior of the glass will always be a connected space.
- There may be leading/trailing whitespace and newlines in the input.
Examples of valid and invalid glasses:
VALID (possible input to your program):
# #
# #
####
# #
# #
# #
# #
# #
# #
##
# #
# #
### #
# #
####
# #
# #
# #
# #
# #
########
# #
# #
# ###
# ###
# ###
#####
INVALID (you won't get one of those as input to your program):
# #
# Has a hole.
####
# #
# # This is also considered a hole.
##
# #
# # Less than two # on a line.
#
## #
# # More than two # on the first line.
###
#
# # Less than two # on the first line.
###
# #
# # # More than one local minimum.
# # # # Liquid might get trapped.
### # #
###
# #
# #
#### Interior is not a connected space.
# #
# #
####
# #
# #######
# ### #
# ## # Has a local maximum.
# # #
# #
# #
######
# #
# #
# #
#####
# # <--- # below the bottom of the glass.
# #
# # # This is also a glass with a hole. The #'s aren't all connected.
# # #
# #
#######
An ugly glass is a glass with liquid just floating around in its interior.
- Liquid is represented by the lowercase letters
a-z
. - There will be no liquid above the first line of
#
characters. This means that it's not required to allow for liquid to fall into the glass. - There may be liquid outside of the glass. This liquid will get discarded when converting the ugly glass into a nice glass.
Examples of ugly glasses:
# y b # i
x v#p q l# l
a # a zj # p g
g #ppcg c#
u # r n # r
##########
Discard Keep Discard
<-- There will never be liquid above the glass
# tz g#
#y abc # d
av z#ox s # l
c#y abth# b
#vg y rm# a
########
e a b c d <-- Discard this as well (not within interior)
A nice glass is a glass where all liquid has accumulated at the bottom.
- From the bottom up, the interior of a nice glass consists of a number of lines that are completely filled with letters, followed by at most one line that's not completely filled with letters, and then a number of lines that are empty.
- There may not be any liquid outside of the interior of a nice glass.
Conversion of an ugly glass into a nice glass
- The liquid inside the glass falls down and accumulates at the bottom.
- Liquid outside of the glass gets discarded.
- When converting an ugly glass into a nice glass, the exact letters in it have to be preserved. For example, if the ugly glass has three
a
's in it, the nice glass has to have threea
's as well. (Soda doesn't suddenly turn into water.) - The letters within the nice glass do not have to be ordered.
- The shape of the glass has to be preserved. No
#
characters may be added or removed. - Any amount of leading/trailing whitespace and newlines is allowed.
Determining glass fullness
- A glass is
full
if its entire interior space is filled with letters. - It is
mostly full
if 50% or more of the interior space is filled. - It's
mostly empty
if less than 50% of the interior space is filled. - It's
empty
if there are no letters in the glass. - There may be any number of additional newlines and spaces between the ASCII art glass and the fullness output.
- The program may output any distinct (but constant!) values for the 4 levels of glass fullness, it doesn't have to print the exact strings above. Please specify which value represents which fullness level.
I/O examples
Example 1 input:
# y b # i
x v#p q l# l
a # a zj # p g
g #ppcg c#
u # r n # r
##########
Example 1 output:
# #
# #
# #
#ppcglqb #
#yprazjnc#
##########
mostly empty
Example 2 input:
# tz g#
#y abc # d
av z#ox s # l
c#y abth# b
#vg y rm# a
########
e a b c d
Example 2 output:
# #
# bc #
#oxysa#
#ygabth#
#vgtyzrm#
########
mostly full
Example 3 input:
# #
# g # f
###ih # d
a c # # e
b ####
Example 3 output:
# #
# #
### g#
#hi#
####
mostly empty
Example 4 input:
#ab#
#cd#
####
Example 4 output:
#cb#
#da#
####
full
Example 5 input:
# # h
# #
a # # g
b# # f
c # #
# # e
d ##
Example 5 output:
# #
# #
# #
# #
# #
# #
##
empty
Example 6 input:
# b az#
#y s ###
###### t
l u
Example 6 output:
# z #
#ybsa###
######
mostly full
Example 7 input:
# # g
# b #f
# c###
#da ### i
# e###
##### h
Example 7 output:
# #
# #
# ###
#de ###
#abc###
#####
mostly empty
Misc
- This is code golf so the shortest answer wins.
- If possible, please provide a link to an online interpreter that can be used to run your program on the provided example inputs, for example tio.run