Water collected between towers: Difference between revisions

Content added Content deleted
m (→‎{{header|Visual Basic .NET}}: changed the name of Rexx 9.3 to Rexx version 3.)
m (→‎{{header|Visual Basic .NET}}: Changed the blocks to double-wide for a less annoying aspect ratio.)
Line 1,226: Line 1,226:
0: 6 7 10 7 6</pre>
0: 6 7 10 7 6</pre>


=={{header|Visual Basic .NET}}==
==={{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.
'''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. I've since modified it to have the same block and wavy characters are the
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 version 3]] output.
[[{{FULLPAGENAME}}#version_3|REXX 9.3]] output, but used the double-wide columns from the task definition area.
<lang vbnet>' Convert tower block data into a string representation, then manipulate that.
<lang vbnet>' Convert tower block data into a string representation, then manipulate that.
Sub Main()
Sub Main()
Line 1,248: Line 1,248:
For j As Integer = 0 To UBound(wta(i))
For j As Integer = 0 To UBound(wta(i))
If wta(i)(j) > 0 Then ' Tower block detected, add block to floor,
If wta(i)(j) > 0 Then ' Tower block detected, add block to floor,
floor &= "" : wta(i)(j) -= 1 : bpf += 1 ' reduce tower by one.
floor &= "██" : wta(i)(j) -= 1 : bpf += 1 ' reduce tower by one.
Else ' Empty space detected, fill when not first or last column.
Else ' Empty space detected, fill when not first or last column.
' "Almost equal to" characters are possible water retention cells.
' "Almost equal to" characters are possible water retention cells.
floor &= If(j > 0 AndAlso j < UBound(wta(i)), "", " ")
floor &= If(j > 0 AndAlso j < UBound(wta(i)), "≈≈", " ")
End If
End If
Next
Next
Line 1,257: Line 1,257:
Loop Until bpf = 0 ' No tower blocks left, so terminate.
Loop Until bpf = 0 ' No tower blocks left, so terminate.
' Now erode potential water retention cells from left and right
' 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
While blk.Contains(" ") : blk = Replace(blk, " ", " ") : End While
While blk.Contains("≈≈ ") : blk = Replace(blk, "≈≈ ", " ") : End While
' Optionally show towers w/ water marks.
' Optionally show towers w/ water marks.
If shoTow Then Console.Write("{0}{1}", lf, blk)
If shoTow Then Console.Write("{0}{1}", lf, Wide(blk))
' Now remove all building blocks and whitespace, leaving only water marks.
' Now remove all building blocks and whitespace, leaving only water marks.
' Then count the remaining characters with the Len() function.
' Then count the remaining characters with the Len() function.
Console.Write("Block {0} retains {1,2} water units.{2}", i + 1,
Console.Write("Block {0} retains {1,2} water units.{2}", i + 1,
Len(Replace(Replace(Replace(blk, lf, ""), "", ""), " ", "")), lf)
Len(Replace(Replace(Replace(blk, lf, ""), "██", ""), " ", "")), lf)
Next
Next
End Sub</lang>
End Sub</lang>
Line 1,275: Line 1,275:
Block 7 retains 0 water units.</lang>
Block 7 retains 0 water units.</lang>
Verbose output shows towers with water ("Almost equal to" characters) left in the "wells" between towers. Just supply any command-line parameter to see it. Use no command line parameters to see the plain output above.
Verbose output shows towers with water ("Almost equal to" characters) left in the "wells" between towers. Just supply any command-line parameter to see it. Use no command line parameters to see the plain output above.
<lang>
<lang> ██
██
██≈≈██
█≈█
██≈≈██
█≈█
██████
███
████████
████
██████████
█████
Block 1 retains 2 water units.
Block 1 retains 2 water units.


██
██
██≈≈≈≈≈≈≈≈██
█≈≈≈≈█
██≈≈██≈≈≈≈██
█≈█≈≈█
██≈≈██≈≈██≈≈████
█≈█≈█≈██
██≈≈██≈≈████████
█≈█≈████
██████≈≈████████
███≈████
████████████████≈≈██
████████≈█
████████████████████
██████████
Block 2 retains 14 water units.
Block 2 retains 14 water units.


██
██≈≈≈≈≈≈≈≈≈≈≈≈≈≈██
█≈≈≈≈≈≈≈█
██≈≈≈≈≈≈██≈≈≈≈≈≈≈≈≈≈≈≈≈≈██
█≈≈≈█≈≈≈≈≈≈≈█
██≈≈██≈≈██≈≈≈≈≈≈≈≈██≈≈████
█≈█≈█≈≈≈≈█≈██
██≈≈██≈≈██≈≈██≈≈≈≈██≈≈██████
█≈█≈█≈█≈≈█≈███
██████≈≈██≈≈██≈≈≈≈██████████
███≈█≈█≈≈█████
████████████≈≈████████████████
██████≈████████
███████████████████████████████
████████████████
Block 3 retains 35 water units.
Block 3 retains 35 water units.


████████
████
████████
████
████████
████
████████
████
████████
████
Block 4 retains 0 water units.
Block 4 retains 0 water units.


██
████
██
██████
███
████████
████
████████
████
████████
████
████████
████
████████
████
Block 5 retains 0 water units.
Block 5 retains 0 water units.


██
██████
███
████████
████
████████
████
████████
████
████████
████
████████
████
████████
████
Block 6 retains 0 water units.
Block 6 retains 0 water units.


██
██
██
██████
███
██████████
█████
██████████
█████
██████████
█████
██████████
█████
██████████
█████
██████████
█████
Block 7 retains 0 water units.</lang>
Block 7 retains 0 water units.</lang>