Jump to content

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. SinceI've thesince ordermodified ofit operationsto is fromhave the bottomsame floorblock toand thewavy top,characters are the representation appears upside-down.
[[{{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 bplbpf As Integer ' Count of tower blocks found per linefloor.
blk = ""
Do
bpf = 0 : Dim floor As String = "" ' string representation of each floor.
bpl = 0
For j As Integer = 0 To UBound(wta(i))
If wta(i)(j) > 0 Then ' Tower block detected, add block to stringfloor,
blkfloor &= "B" : wta(i)(j) -= 1 : bplbpf += 1 ' reduce tower by one.
Else ' Empty space detected, fill ifwhen not first or last column.
' "Almost equal to" Periodscharacters are possible water retention cells.
blkfloor &= If(j > 0 AndAlso j < UBound(wta(i)), ".", " ")
End If
Next
If bplbpf > 0 Then blk &= lf floor & lf & blk ' Add floors until no blocks are leftgone.
Loop Until bplbpf = 0 ' No tower blocks left, so terminate.
' 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
' Optionally show towers w/ water marks.
If shoTow Then Console.WriteLineWrite(lf"{0}{1}", &lf, blk)
' 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
End Sub</lang>
Line 1,272 ⟶ 1,274:
Block 6 retains 0 water units.
Block 7 retains 0 water units.</lang>
Verbose output shows upside-down buildingstowers with water (periods"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
BBBB
█≈█
BBB
█≈█
B.B
███
B.B
████
B
█████
B
 
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.
 
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.
 
████
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBBB
 
Block 4 retains 0 water units.
 
BBBB
██
BBBB
███
BBBB
████
BBBB
████
BBBB
████
BBB
████
BB
████
B
 
Block 5 retains 0 water units.
 
BBBB
███
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBBB
████
BBB
████
B
 
Block 6 retains 0 water units.
 
BBBBB
BBBBB
BBBBB
███
BBBBB
█████
BBBBB
█████
BBBBB
█████
BBB
█████
B
█████
B
█████
B
 
Block 7 retains 0 water units.</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.