15
\$\begingroup\$

In this challenge, you must display ASCII art of a water balloon given the amount of water that the balloon is filled with:

|  __||__  |
| / #   #\ |
| |######| |
| |######| |
| |######| |
| |######| |
| |######| |
| \######/ |
|          |
|          |
+----------+

How to draw the balloon

To display a balloon of size n, follow the following steps (note: whenever the division symbol (/) is used, it represents integer division, rounding down):

  1. Draw a container consisting of ten vertical bars (|) on the left and right, ten dashes (-) on the bottom, and a plus sign (+) in the bottom left and bottom right corner. This makes the whole thing 12x11, and the "inside" 10x10.

    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    +----------+
    
  2. Draw two vertical bars (the opening of the balloon) centered in the middle of the top row, with n/2 underscores (_) on either side (for this example, n will be 5):

    |  __||__  |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    +----------+
    
  3. Draw one slash (/) and one backslash (\) surrounding this top row, one row below:

    |  __||__  |
    | /      \ |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    +----------+
    
  4. Draw n rows of identically-spaced vertical bars, and then one row of a pair of (still identically-spaced) backslash and slash:

    |  __||__  |
    | /      \ |
    | |      | |
    | |      | |
    | |      | |
    | |      | |
    | |      | |
    | \      / |
    |          |
    |          |
    +----------+
    
  5. "Fill" the balloon with water, represented by a hash sign (#). Start at the lowest row, and work upwards. If a row is not entirely filled, you may place the hash marks wherever you want (in the below example, they are placed randomly, but you could put them, say, all on the left side if you want).

    |  __||__  |
    | / #   #\ |
    | |######| |
    | |######| |
    | |######| |
    | |######| |
    | |######| |
    | \######/ |
    |          |
    |          |
    +----------+
    

The maximum n is 7, and the minimum is 0.

Input

The input will be an integer i, which is the amount of hash marks (water) that must be drawn.

It will never be less than 2, or greater than 100.

Output

The output should be a balloon of size n containing i hash marks (units of water), where n is the lowest possible size that can hold i units of water. Since i will always be 2 or greater, n will always be 0 or greater.

The maximum possible size a balloon can be drawn at is n = 7. If a size 7 balloon cannot fit the amount of water specified, the balloon pops:

|          |
|          |
|##  #  ###|
|##########|
|##########|
|##########|
|##########|
|##########|
|##########|
|##########|
+----------+

(The above should be the output for input i = 76. Just like the unpopped balloon, the six extra units of water on the top row may be arranged however you please.)

Test cases

Why have one test case, when you can have all of them?

Here's an animated GIF of all inputs i from 2 to 100:

animation of all i from 2 to 100

Scoring

This is , so the shortest code in bytes wins.

\$\endgroup\$
1

2 Answers 2

2
\$\begingroup\$

Octave, 523 bytes

23 of those bytes are just for the n=100 case. Maybe someone can suggest a more efficient way...

n=input(0);x=zeros(11,6)+32;x(:,1)=124;x(11,:)=45;x(11,1)=43;
if n<5
w=3;h=2;a=2;
elseif n<7
w=3;h=3;a=2;
elseif n<17
w=4;h=4;a=4;
elseif n<37
w=5;h=6;a=6;
else
w=6;h=9;a=8;
end
if n<73
x(1,6)=124;x(1,9-w:5)=95;x(2,8-w)=47;x(3:1+h,8-w)=124;x(1+h,8-w)=92;x(2:1+h,9-w:6)=35;x=[x,fliplr(x)];x(2,5+w)=92;x(1+h,5+w)=47;x(2:1+floor((a*h-n)/a),9-w:4+w)=32;x(2+floor((a*h-n)/a),9-w+a-mod(a-n,a):4+w)=32;
else
x=[x,fliplr(x)];x(max(1,ceil((100-n)/10)):10,2:11)=35; if (n<100) x(ceil((100-n)/10),(2+mod(n,10)):11)=32; end
end
char(x)

Test

Input: 21

Output:

|  __||__  |
| /      \ |
| |      | |
| |###   | |
| |######| |
| |######| |
| \######/ |
|          |
|          |
|          |
+----------+
\$\endgroup\$
2
\$\begingroup\$

Python 2, 591 bytes

Took me some time and it probably could be golfed much more.

Hope there aren't any major errors.

r=[list(x)for x in ("|          |!"*10+"+----------+").split('!')]
s,t=[0]*4+[1]*2+[2]*10+[3]*4+[4]*16+[5]*6+[6]*22+[7]*8+[8]*29,[(4,2,2),(4,3,2),(3,4,4),(3,5,4),(2,6,6),(2,7,6),(1,8,8),(1,9,8),(0,9,10)]
a,b,c,d,e='|','/','\\','_','#'
def p(x,y,w):r[y][x]=w
def q(l):
 h,j,u=l
 p(5,0,a);p(6,0,a)
 for o in range(4-h):p(h+o+1,0,d);p(h+u-o,0,d)
 p(h,1,b);p(h+u+1,1,c)
 for o in range(j-2):p(h,o+2,a);p(h+u+1,o+2,a)
 p(h,j,c);p(h+u+1,j,b)
def w(i,l):
 h,j,u=l
 for o in range(i):x,y=o%u,o/u;p(h+x+1,j-y,e)
def f(i):
 n=s[i]
 l=t[n]
 if n<8:q(l)
 w(i,l)
 print "\n".join(["".join(x)for x in r])

Example run:

f(34)

gives:

|  __||__  |
| /####  \ |
| |######| |
| |######| |
| |######| |
| |######| |
| \######/ |
|          |
|          |
|          |
+----------+
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.