How to resolve the algorithm Water collected between towers step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Water collected between towers step by step in the REXX programming language

Table of Contents

Problem Statement

In a two-dimensional world, we begin with any bar-chart (or row of close-packed 'towers', each of unit width), and then it rains, completely filling all convex enclosures in the chart with water.

In the example above, a bar chart representing the values [5, 3, 7, 2, 6, 4, 5, 9, 1, 2] has filled, collecting 14 units of water. Write a function, in your language, from a given array of heights, to the number of water units that can be held in this way, by a corresponding bar chart. Calculate the number of water units that could be collected by bar charts representing each of the following seven series:

See, also:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Water collected between towers step by step in the REXX programming language

Source code in the rexx programming language

/* REXX */
Call bars '1 5 3 7 2'
Call bars '5 3 7 2 6 4 5 9 1 2'
Call bars '2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1'
Call bars '5 5 5 5'
Call bars '5 6 7 8'
Call bars '8 7 7 6'
Call bars '6 7 10 7 6'
Exit
bars:
Parse Arg bars
bar.0=words(bars)
high=0
box.=' '
Do i=1 To words(bars)
  bar.i=word(bars,i)
  high=max(high,bar.i)
  Do j=1 To bar.i
    box.i.j='x'
    End
  End
m=1
w=0
Do Forever
  Do i=m+1 To bar.0
    If bar.i>bar.m Then
      Leave
    End
  If i>bar.0 Then Leave
  n=i
  Do i=m+1 To n-1
    w=w+bar.m-bar.i
    Do j=bar.i+1 To bar.m
      box.i.j='*'
      End
    End
  m=n
  End
m=bar.0
Do Forever
  Do i=bar.0 To 1 By -1
    If bar.i>bar.m Then
      Leave
    End
  If i<1 Then Leave
  n=i
  Do i=m-1 To n+1 By -1
    w=w+bar.m-bar.i
    Do j=bar.i+1 To bar.m
      box.i.j='*'
      End
    End
  m=n
  End
Say bars '->' w
Call show
Return
show:
Do j=high To 1 By -1
  ol=''
  Do i=1 To bar.0
    ol=ol box.i.j
    End
  Say ol
  End
Return


/*REXX program calculates and displays the amount of rainwater collected between towers.*/
       call tower  1  5  3  7  2
       call tower  5  3  7  2  6  4  5  9  1  2
       call tower  2  6  3  5  2  8  1  4  2  2  5  3  5  7  4  1
       call tower  5  5  5  5
       call tower  5  6  7  8
       call tower  8  7  7  6
       call tower  6  7 10  7  6
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tower: procedure; arg y;  #=words(y); t.=0; L.=0 /*the T. array holds the tower heights.*/
            do j=1  for #;    t.j= word(y, j)    /*construct the towers,                */
            _= j-1;           L.j= max(t._, L._) /*    "      "  left─most tallest tower*/
            end   /*j*/
       R.=0
            do b=#  by -1  for #;  _= b+1; R.b= max(t._, R._) /*right─most tallest tower*/
            end   /*b*/
       w.=0                                                       /*rainwater collected.*/
            do f=1  for #;  if t.f>=L.f | t.f>=R.f  then iterate  /*rain between towers?*/
            w.f= min(L.f, R.f) - t.f;     w.00= w.00 + w.f        /*rainwater collected.*/
            end   /*f*/
       say right(w.00, 9) 'units of rainwater collected for: '  y /*display water units.*/
       return


/*REXX program calculates and displays the amount of rainwater collected between towers.*/
       call tower  1  5  3  7  2
       call tower  5  3  7  2  6  4  5  9  1  2
       call tower  2  6  3  5  2  8  1  4  2  2  5  3  5  7  4  1
       call tower  5  5  5  5
       call tower  5  6  7  8
       call tower  8  7  7  6
       call tower  6  7 10  7  6
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tower: procedure; arg y; #= words(y); t.=0; L.=0 /*the T. array holds the tower heights.*/
            do j=1  for #;  t.j=word(y,j); _=j-1 /*construct the towers;  max height.   */
            L.j=max(t._, L._); t.0=max(t.0, t.j) /*left-most tallest tower; build scale.*/
            end   /*j*/
       R.=0
            do b=#  by -1  for #; _= b+1;  R.b= max(t._, R._) /*right-most tallest tower*/
            end   /*b*/
       w.=0                                                       /*rainwater collected.*/
            do f=1  for #;  if t.f>=L.f | t.f>=R.f  then iterate  /*rain between towers?*/
            w.f= min(L.f, R.f) - t.f;     w.00= w.00 + w.f        /*rainwater collected.*/
            end   /*f*/
       if w.00==0  then w.00= 'no'               /*pretty up wording for "no rainwater".*/
       ratio= 2                                  /*used to maintain a good aspect ratio.*/
       p.=                                       /*P.  stores plot versions of towers.  */
            do c=0  to #;  cc= c * ratio         /*construct the plot+scale for display.*/
              do h=1  for t.c+w.c;    glyph= '█' /*maybe show a floor of some tower(s). */
                       if h>t.c  then glyph= '≈' /*  "     "  rainwater between towers. */
              if c==0  then p.h= overlay(right(h, 9)         , p.h,  1   ) /*tower scale*/
                       else p.h= overlay(copies(glyph,ratio) , p.h, 10+cc) /*build tower*/
              end   /*h*/
            end     /*c*/
       p.1= overlay(w.00  'units of rainwater collected', p.1, 15*ratio+#) /*append text*/
            do z=t.0  by -1  to 0;     say p.z   /*display various tower floors & water.*/
            end     /*z*/
       return


  

You may also check:How to resolve the algorithm Perfect numbers step by step in the SASL programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Scala programming language
You may also check:How to resolve the algorithm Own digits power sum step by step in the 11l programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Racket programming language