Water collected between towers: Difference between revisions
Content added Content deleted
mNo edit summary |
(Added vb.Net program, features erosion of strings method instead of tower height comparison method,) |
||
Line 1,190: | Line 1,190: | ||
0: 8 7 7 6 |
0: 8 7 7 6 |
||
0: 6 7 10 7 6</pre> |
0: 6 7 10 7 6</pre> |
||
=={{header|Visual Basic .NET}}== |
|||
'''Method:''' Instead of "scanning" adjoining towers for each column, convert the tower data into a string representation with building blocks, empty spaces, and potential water retention sites. Then "erode" away the water retention sites that are unsupported. This is accomplished with the String Replace() function. The replace operations are unleashed upon the entire "block" of towers, rather than a cell at a time or a line at a time - which perhaps increases the program's execution-time, but reduces program's complexity. |
|||
The program can optionally display the interim string representation of each tower block before the final count is completed. Since the order of operations is from the bottom floor to the top, the representation appears upside-down. |
|||
<lang vbnet>' Convert tower block data into a string representation, then manipulate that. |
|||
Sub Main() |
|||
Dim shoTow As Boolean = Environment.GetCommandLineArgs().Count > 1 ' Show towers. |
|||
Dim wta(,) As Integer = {{1, 5, 3, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
|||
{5, 3, 7, 2, 6, 4, 5, 9, 1, 2, 0, 0, 0, 0, 0, 0}, |
|||
{2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1}, |
|||
{5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
|||
{5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
|||
{8, 7, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
|||
{6, 7, 10, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} |
|||
Dim blk As String, ' String representation of a block of towers. |
|||
lf As String = vbCrLf ' Line feed to separate floors in block of towers. |
|||
For i As Integer = 0 To UBound(wta, 1) |
|||
Dim ctb As Integer = -1, ' Count of tower blocks found per line. |
|||
lc As Integer, ' Previous count. |
|||
lim As Integer = UBound(wta, 2) ' Limit of block length. |
|||
blk = "" |
|||
Do |
|||
lc = ctb : ctb = 0 |
|||
For j As Integer = 0 To lim |
|||
If wta(i, j) > 0 Then ' Tower block detected, add block to string, |
|||
blk &= "B" : wta(i, j) -= 1 : ctb += 1 ' reduce tower by one. |
|||
Else ' Empty space detected, fill if not first or last column. |
|||
' Periods are possible water retention cells. |
|||
blk &= If(j > 0 AndAlso j < lim, ".", " ") |
|||
End If |
|||
Next |
|||
If lc < 0 Then ' Set new limit on first floor, |
|||
lim = Math.Max(lc, ctb) - 1 ' and trim initial string. |
|||
blk = Mid(blk, 1, Len(blk) - (UBound(wta, 2) - lim)) & lf |
|||
Else |
|||
If ctb > 0 Then blk &= lf ' Add floors until no blocks are left. |
|||
End If |
|||
Loop Until ctb = 0 ' No tower blocks left, so terminate. |
|||
' Now erode potential water retention cells from left and right |
|||
While blk.Contains(" .") : blk = Replace(blk, " .", " ") : End While |
|||
While blk.Contains(". ") : blk = Replace(blk, ". ", " ") : End While |
|||
' Optionally show towers w/ water marks. |
|||
If shoTow Then Console.WriteLine(lf & blk) |
|||
' Lastly, remove everything except the water marks, |
|||
' then count the remaining characters with Len(). |
|||
Console.WriteLine("Block {0} retains {1,2} water units.", i + 1, |
|||
Len(Replace(Replace(Replace(blk, lf, ""), "B", ""), " ", ""))) |
|||
Next |
|||
End Sub</lang> |
|||
{{out}}<lang>Block 1 retains 2 water units. |
|||
Block 2 retains 14 water units. |
|||
Block 3 retains 35 water units. |
|||
Block 4 retains 0 water units. |
|||
Block 5 retains 0 water units. |
|||
Block 6 retains 0 water units. |
|||
Block 7 retains 0 water units.</lang> |
|||
Verbose output shows upside-down buildings with water (periods) left in "wells". Just supply any command-line parameter to see it. |
|||
<lang>BBBBB |
|||
BBBB |
|||
BBB |
|||
B.B |
|||
B.B |
|||
B |
|||
B |
|||
Block 1 retains 2 water units. |
|||
BBBBBBBBBB |
|||
BBBBBBBB.B |
|||
BBB.BBBB |
|||
B.B.BBBB |
|||
B.B.B.BB |
|||
B.B..B |
|||
B....B |
|||
B |
|||
B |
|||
Block 2 retains 14 water units. |
|||
BBBBBBBBBBBBBBBB |
|||
BBBBBB.BBBBBBBB |
|||
BBB.B.B..BBBBB |
|||
B.B.B.B..B.BBB |
|||
B.B.B....B.BB |
|||
B...B.......B |
|||
B.......B |
|||
B |
|||
Block 3 retains 35 water units. |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
Block 4 retains 0 water units. |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBB |
|||
BB |
|||
B |
|||
Block 5 retains 0 water units. |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBBB |
|||
BBB |
|||
B |
|||
Block 6 retains 0 water units. |
|||
BBBBB |
|||
BBBBB |
|||
BBBBB |
|||
BBBBB |
|||
BBBBB |
|||
BBBBB |
|||
BBB |
|||
B |
|||
B |
|||
B |
|||
Block 7 retains 0 water units.</lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |