Water collected between towers: Difference between revisions
→{{header|Visual Basic .NET}}: Modified it to display the tower blocks right side up, changed characters to match the REXX 9.3 output.
(→{{header|C sharp|C#}}: added using dependencies, modified code to show towers upright instead of upside-down. Changed characters to match REXX 9.3 output when showing tower blocks.) |
(→{{header|Visual Basic .NET}}: Modified it to display the tower blocks right side up, changed characters to match the REXX 9.3 output.) |
||
Line 1,229:
'''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.
[[{{FULLPAGENAME}}#version_3|REXX 9.3]] output.
<lang vbnet>' Convert tower block data into a string representation, then manipulate that.
Sub Main()
Line 1,241 ⟶ 1,242:
lf As String = vbCrLf ' Line feed to separate floors in a block of towers.
For i As Integer = 0 To UBound(wta)
Dim
blk = ""
Do
bpf = 0 : Dim floor As String = "" ' string representation of each floor.
For j As Integer = 0 To UBound(wta(i))
If wta(i)(j) > 0 Then ' Tower block detected, add block to
Else '
' "Almost equal to"
End If
Next
If
Loop Until
' Now erode potential water retention cells from left and right
While blk.Contains("
While blk.Contains("
' Optionally show towers w/ water marks.
If shoTow Then Console.
' Now remove all building blocks and whitespace, leaving only water marks.
' Then count the remaining characters with the Len() function.
Console.Write("Block {0} retains {1,2} water units.{2}", i + 1,
Len(Replace(Replace(Replace(blk, lf, ""), "█", ""), " ", "")), lf)
Next
End Sub</lang>
Line 1,272 ⟶ 1,274:
Block 6 retains 0 water units.
Block 7 retains 0 water units.</lang>
Verbose output shows
<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>
|