Intro
Consider a grid of the characters f A\/
such as
f f f
A
A / \
\ / A
A \/
/
\/
where:
f
represents a faucet that pours a stream of water downwardA
bifurcates the stream of water above so exactly half goes left and exactly half goes right\
shifts the stream of water above to the right by one unit/
shifts the stream of water above to the left by one unit- the combinations
\/
creates a trough with infinite capacity that collects the water streams above it [space]
is empty space than the water can move through
From this we can imagine the path the water (*
) would take as it comes out of the faucets and falls either into the troughs or out of the grid area:
f f f <-- first second and third faucets
* * *A*
* *A*/ \*
\*/ * *A <-- a '*' is not drawn to the right of this A because it would be out of the 9×7 bounds
* *A*\/ <-- upper trough
**/ *
\/ * <-- lower trough
Assuming the 3 faucets output the same amount of water one at a time we can see that
- All of the first faucet's water goes to the lower trough.
- One half of the second faucet's water goes to the lower trough and the other half is split between the lower trough and falling off the grid.
- One quarter of the third faucet's water goes to the lower trough, one quarter falls off the bottom of the grid, one quarter goes into the upper trough, and one quarter falls off the grid to the right.
From this we can tell that (1 + 3/4 + 1/4 + 1/4) / 3 = 75%
of the water is caught by the troughs and (1/4 + 1/4 + 1/4) / 3 = 25%
falls off the grid.
Challenges
You may complete any or all of these challenges relating to this ASCII water flow setup. They are all code-golf, the shortest answer for each challenge is the winner. The accepted answer will be the person who completes the most challenges, with total code length as tie-breaker.
Challenge 1
Write a program that outputs the fraction of water that flows into troughs for a given grid. The output of the example above would simply be 0.75
.
Challenge 2
Write a program that, given a grid, draws the *
's in the places water flows as I've done above. You should not overwrite anything besides space characters and the grid should not change size. So for something like
f
/A
nothing needs to be done since, although water does flow on either side of the A, it can't be drawn to the left without removing the /
and it can't be drawn to the right without making the 2×2 grid bigger.
Challenge 3 (Updated)
Write a program that takes in two non-negative integers, the total T and the amount to keep K (T >= K). Generate and draw a grid with exactly one f
such that when that faucet pours out T units of water, exactly K will flow into troughs. If it is impossible to do this in a finite grid for a particular (T, K) pair then output 'Impossible'.
Clarifications (apply to all challenges)
- Input can be via stdin, or a file, or even a function call on the string representation of the grid. Just make it obvious how to run different inputs.
- Output must go to stdout.
\A
andA/
andAA
are also troughs as you'd expect.- A w by h grid will always be a well formatted rectangle of w*h characters not counting newlines. There will be no missing trailing spaces and no occurrences of
*
. - The grid dimensions can be as small as 1×1 and arbitrarily large. (Arbitrarily large within reason, int.maxValue or the like is an acceptable limit. Same goes for T and K.)
- A stream above an
f
flows right through it. - The faucets can be anywhere, not just on the top row.
A
always divides the amount of water poured on it exactly in half.
Note: Things like /A
and //
are perfectly valid. The water does freely flow between the characters (though for challenge 2 there's not enough room to draw it).
So, in the setup
ff
/A
The left f
stream pours down, hits the /
and shifts left. The right f
stream pours down, hits the A
, half goes right and half goes left between the A
and the /
.
e.g.
ff
**
*/A*
** *
** *
f
s \$\endgroup\$/A
if water falls onto theA
. For all challenges, it would be good to clarify whether\A
is a trough. For the third challenge, should 3 units falling on anA
be assumed to split1.5 / 1.5
(so the input is really a single rational number) or is it2 / 1
, in which case which side receives the2
? \$\endgroup\$A
then both sides do get 1.5. It's up to the coder to make sure float precision is not an issue.) \$\endgroup\$