Water collected between towers: Difference between revisions

Content added Content deleted
(→‎{{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: 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.
'''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.
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.
<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,241: Line 1,242:
lf As String = vbCrLf ' Line feed to separate floors in 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)
For i As Integer = 0 To UBound(wta)
Dim bpl As Integer ' Count of tower blocks found per line.
Dim bpf As Integer ' Count of tower blocks found per floor.
blk = ""
blk = ""
Do
Do
bpf = 0 : Dim floor As String = "" ' string representation of each floor.
bpl = 0
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 string,
If wta(i)(j) > 0 Then ' Tower block detected, add block to floor,
blk &= "B" : wta(i)(j) -= 1 : bpl += 1 ' reduce tower by one.
floor &= "" : wta(i)(j) -= 1 : bpf += 1 ' reduce tower by one.
Else ' Empty space detected, fill if not first or last column.
Else ' Empty space detected, fill when not first or last column.
' Periods are possible water retention cells.
' "Almost equal to" characters are possible water retention cells.
blk &= If(j > 0 AndAlso j < UBound(wta(i)), ".", " ")
floor &= If(j > 0 AndAlso j < UBound(wta(i)), "", " ")
End If
End If
Next
Next
If bpl > 0 Then blk &= lf ' Add floors until no blocks are left.
If bpf > 0 Then blk = floor & lf & blk ' Add floors until blocks are gone.
Loop Until bpl = 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.WriteLine(lf & blk)
If shoTow Then Console.Write("{0}{1}", lf, 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.
Console.WriteLine("Block {0} retains {1,2} water units.", i + 1,
Console.Write("Block {0} retains {1,2} water units.{2}", i + 1,
Len(Replace(Replace(Replace(blk, lf, ""), "B", ""), " ", "")))
Len(Replace(Replace(Replace(blk, lf, ""), "█", ""), " ", "")), lf)
Next
Next
End Sub</lang>
End Sub</lang>
Line 1,272: Line 1,274:
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>
Verbose output shows upside-down buildings with water (periods) left in "wells". Just supply any command-line parameter to see it.
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>BBBBB
<lang>
BBBB
█≈█
BBB
█≈█
B.B
███
B.B
████
B
█████
B

Block 1 retains 2 water units.
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.
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.
Block 3 retains 35 water units.


████
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBBB

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


BBBB
██
BBBB
███
BBBB
████
BBBB
████
BBBB
████
BBB
████
BB
████
B

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


BBBB
███
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBB
████
B

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


BBBBB
BBBBB
BBBBB
███
BBBBB
█████
BBBBB
█████
BBBBB
█████
BBB
█████
B
█████
B
█████
B

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