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. |
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 |
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 |
If wta(i)(j) > 0 Then ' Tower block detected, add block to floor, |
||
floor &= "█" : wta(i)(j) -= 1 : bpf += 1 ' reduce tower by one. |
|||
Else ' |
Else ' Empty space detected, fill when not first or last column. |
||
' |
' "Almost equal to" characters are possible water retention cells. |
||
floor &= If(j > 0 AndAlso j < UBound(wta(i)), "≈", " ") |
|||
End If |
End If |
||
Next |
Next |
||
If |
If bpf > 0 Then blk = floor & lf & blk ' Add floors until blocks are gone. |
||
Loop Until |
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(" |
While blk.Contains(" ≈") : blk = Replace(blk, " ≈", " ") : End While |
||
While blk.Contains(" |
While blk.Contains("≈ ") : blk = Replace(blk, "≈ ", " ") : End While |
||
' Optionally show towers w/ water marks. |
' Optionally show towers w/ water marks. |
||
If shoTow Then Console. |
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 |
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> █ |
||
█ |
|||
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> |
||