Water collected between towers: Difference between revisions

Added C# program, translation of vb.NET
(→‎{{header|Visual Basic .NET}}: simplified water tower array (wta) declaration, streamlined code to take advantage of preset sub-array lengths.)
(Added C# program, translation of vb.NET)
Line 303:
0 : 6,7,10,7,6
</pre>
 
=={{header|C sharp|C#}}==
Translation from [[{{FULLPAGENAME}}#Visual_Basic_.NET|Visual Basic .NET]]. See that entry for code comments and details.
<lang Csharp>static void Main(string[] args)
{
int[][] wta = {
new int[] {1, 5, 3, 7, 2}, new int[] { 5, 3, 7, 2, 6, 4, 5, 9, 1, 2 },
new int[] { 2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1 },
new int[] { 5, 5, 5, 5 }, new int[] { 5, 6, 7, 8 },
new int[] { 8, 7, 7, 6 }, new int[] { 6, 7, 10, 7, 6 }};
string blk = "", lf = "\n"; for (int i = 0; i < wta.Length; i++)
{
int bpl = -1; blk = ""; do
{
bpl = 0; for (int j = 0; j < wta[i].Length; j++)
{
if (wta[i][j] > 0)
{ blk += "B"; wta[i][j] -= 1; bpl += 1; }
else blk += j > 0 && j < wta[i].Length - 1 ? "." : " ";
}
if (bpl > 0) blk += lf;
} while (bpl > 0);
while (blk.Contains(" .")) blk = blk.Replace(" .", " ");
while (blk.Contains(". ")) blk = blk.Replace(". ", " ");
if (args.Length > 0) Console.WriteLine(lf + blk);
Console.WriteLine("Block {0} retains {1,2} water units.", i + 1,
(blk.Replace(lf, "").Replace("B", "").Replace(" ", "")).Length);
}
}
</lang>{{out}}<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>
 
=={{header|Go}}==
Line 390 ⟶ 426:
0
</pre>
 
 
 
=={{header|Haskell}}==