Water collected between towers: Difference between revisions

m
→‎{{header|Visual Basic .NET}}: Converted version 1 to .NET core, added "Module" constructs around both versions
m (→‎{{header|Visual Basic .NET}}: Converted version 1 to .NET core, added "Module" constructs around both versions)
Line 2,074:
=={{header|Visual Basic .NET}}==
===Version 1===
'''Method:''' Instead of "scanning" adjoining towers for each column, convertthis routine converts the tower data into a string representation with building blocks, empty spaces, and potential water retention sites. ThenThe potential water retention sites are then "erodeeroded" away thewhere waterthey retentionare sitesfound thatto arebe 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. I've since modified it to have the same block and wavy characters are the
[[{{FULLPAGENAME}}#version_3|REXX 9.3]] output, but used the double-wide columns, as pictured in the task definition area.
<lang vbnet>' Convert tower block data into a string representation, then manipulate that.
Module Module1
Sub Main()
Sub Main(Args() As String)
Dim shoTow As Boolean = Environment.GetCommandLineArgs().Count > 1 ' Show towers.
Dim wta As Integer()() = { ' water tower array (input data).
NewDim wta As Integer()() = {1, 5, 3, 7, 2}, New Integer() {5, 3, 7, 2, 6, 4, 5, 9, 1, 2}, ' Water tower array (input data).
New Integer() {21, 65, 3, 57, 2}, 8,New 1Integer() {5, 43, 27, 2, 56, 34, 5, 79, 41, 12},
New Integer() {52, 56, 53, 5}, New2, Integer()8, 1, 4, 2, 2, {5, 63, 5, 7, 84, 1},
New Integer() {85, 75, 75, 65}, New Integer() {65, 7, 106, 7, 6}8},
New Integer() {8, 7, 7, 6}, New Integer() {6, 7, 10, 7, 6}}
Dim blk As String, ' String representation of a block of towers.
lfDim blk As String, = vbCrLf ' Line feed to separate floors in ' String representation of a block of towers.
Dim blk As String, lf As String = vbLf, ' Line feed to ' Stringseparate representationfloors ofin a block of towers.
For i As Integer = 0 To UBound(wta)
Dim bpf As Integer tb = "██", wr = "≈≈", mt = " " ' Count ofTower towerBlock, blocksWater foundRetained, pereMpTy floorspace.
blkFor i As Integer = ""0 To wta.Length - 1
Dim bpf As Integer ' Count of tower blocks found per floor.
Do
blk = ""
bpf = 0 : Dim floor As String = "" ' string representation of each floor.
For j As Integer = 0 To UBound(wta(i))Do
Ifbpf wta(i)(j) >= 0 Then: Dim floor As String = '"" Tower block' detected,String addrepresentation blockof toeach floor,.
For j As Integer floor &= "██"0 :To wta(i)(j).Length -= 1 : bpf += 1 ' reduce tower by one.
Else ' If wta(i)(j) > 0 EmptyThen space detected, fill when not first' orTower lastblock column.detected, add block to floor,
' "Almost equal to" charactersfloor &= tb : wta(i)(j) -= 1 : bpf += 1 ' are possiblereduce watertower retentionby cellsone.
floorElse &= If(j' > 0 AndAlso j < UBound(wta(i)),Empty "≈≈"space detected, "fill when not first or last ")column.
End floor &= If(j > 0 AndAlso j < wta(i).Length - 1, wr, mt)
Next End If
Next
If bpf > 0 Then blk = floor & lf & blk ' Add floors until blocks are gone.
Loop Until bpf = 0 If bpf > 0 Then blk = floor & lf & blk ' Add floors ' No toweruntil blocks left, soare terminategone.
Dim wta As Integer()() Loop Until bpf = {0 ' waterNo tower arrayblocks left, (inputso data)terminate.
' Now erode ' Erode potential water retention cells from left and right.
While blk.Contains(" ≈≈") : blk = Replace(blk, " ≈≈", " ") : End While
While blk.Contains("≈≈mt & "wr) : blk = blk.Replace(blk,mt "≈≈& "wr, " mt & "mt) : End While
While blk.Contains("wr & ≈≈"mt) : blk = blk.Replace(blk,wr "& ≈≈"mt, " mt & "mt) : End While
' OptionallyOptionaly show towers w/ water marks.
If shoTow Then Console.Write("{0}{1}", lf, Wide(blk))
' Now remove all building blocks and whitespace, leaving only water marks.
' Then count ' Subtract the remainingamount of non-water mark characters withfrom the Len()total char functionamount.
Console.Write("Block {0} retains {1,2} water units.{2}", i + 1,
(Len(blk).Length - Len(blk.Replace(blkwr, "≈≈", "")).Length) \ 2, lf)
Next
End Sub</lang>
End Module</lang>
{{out}}<lang>Block 1 retains 2 water units.
Block 2 retains 14 water units.
Line 2,193 ⟶ 2,194:
'''Method:''' More conventional "scanning" method. A Char array is used, but no Replace() statements. Output is similar to version 1, although there is now a left margin of three spaces, the results statement is immediately to the right of the string representation of the tower blocks (instead of underneath), the verb is "hold(s)" instead of "retains", and there is a special string when the results indicate zero.
 
<lang vbnet>Module ''' <summary>Module1
''' <summary>
''' wide - Widens the aspect ratio of a linefeed separated string.
''' </summary>
Line 2,289 ⟶ 2,291:
Console.WriteLine(report(block, blkCntr, verb, shoTow))
Next
End Sub</lang>
End Module</lang>
Regular version 2 output:
<lang> Block 1 holds 2 water units.