Jump to content

Water collected between towers: Difference between revisions

→‎{{header|Visual Basic .NET}}: simplified water tower array (wta) declaration, streamlined code to take advantage of preset sub-array lengths.
(Added vb.Net program, features erosion of strings method instead of tower height comparison method,)
(→‎{{header|Visual Basic .NET}}: simplified water tower array (wta) declaration, streamlined code to take advantage of preset sub-array lengths.)
Line 1,198:
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}, ' water tower array (input data).
New Integer() {1, 5, 3, 7, 2}, New Integer() {5, 3, 7, 2, 6, 4, 5, 9, 1, 2, 0, 0, 0, 0, 0, 0},
New Integer() {2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1},
New Integer() {5, 5, 5, 5}, 0,New 0,Integer() 0, 0, 0, 0, 0, 0, 0{5, 06, 07, 08},
New Integer() {58, 67, 7, 86}, 0,New 0,Integer() 0{6, 07, 010, 07, 0, 0, 0, 0, 0, 06}},
{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 a block of towers.
For i As Integer = 0 To UBound(wta, 1)
Dim ctbbpl 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 : ctbbpl = 0
For j As Integer = 0 To limUBound(wta(i))
If wta(i, )(j) > 0 Then ' Tower block detected, add block to string,
blk &= "B" : wta(i, )(j) -= 1 : ctbbpl += 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 < limUBound(wta(i)), ".", " ")
End If
Next
If lcbpl <> 0 Then blk &= lf ' Add floors until no blocks are ' Set new limit on first floor,left.
Loop Until bpl = 0 lim = Math.Max(lc, ctb) - 1 ' No 'tower blocks and trimleft, initialso stringterminate.
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
Line 1,234 ⟶ 1,225:
' Optionally show towers w/ water marks.
If shoTow Then Console.WriteLine(lf & blk)
' Lastly,Now remove everythingall exceptbuilding theblocks waterand markswhitespace, leaving only 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", ""), " ", "")))
Cookies help us deliver our services. By using our services, you agree to our use of cookies.