Solution of Google Lake Volume Puzzle

6 points by graycat ↗ HN
Below we have output and code for the Google lake volume puzzle.

1 comment

[ 3.4 ms ] story [ 14.0 ms ] thread
Below we have output and code for the Google lake volume puzzle.

     >glv_puzzle 4 9 7 5 9 7 4 8 3 4 2 6 5 8 12 0 0 0 3 6 5 8 9 0 2 5 3
     GLV_puzzle:  Started ...
     GLV_puzzle:  Number of walls = 27
     GLV_puzzle:  Max wall height = 12
     GLV_puzzle:  Max wall height at 15
     GLV_puzzle.  Calculating left results.
     GLV_puzzle.  Calculating right results.
     GLV_puzzle:  Lake volume = 89

     PRINT_LAKE:  We display the lake with walls 'X' and water '.'

                   X
                   X
                   X
      X..X.........X.......X
      X..X..X.....XX......XX
      XX.XX.X.....XX......XX
      XX.XX.X...X.XX....X.XX
      XXXXX.X...XXXX....XXXX..X
     XXXXXXXX.X.XXXX....XXXX..X
     XXXXXXXXXX.XXXX...XXXXX..XX
     XXXXXXXXXXXXXXX...XXXXX.XXX
     XXXXXXXXXXXXXXX...XXXXX.XXX

     GLV_puzzle:  Returning.


     /* GLV_PUZZLE.REX -- Google lake volume puzzle.                       */
     /*                                                                    */
     /*   Here we have code for a solution to                              */
     /*   the Google lake volume puzzle.                                   */
     /*                                                                    */
     /*   The code is in Open Object Rexx and                              */
     /*   easy to read.                                                    */
     /*                                                                    */
     /*   The lake is one foot wide and, for                               */
     /*   some positive integer n, n feet long.                            */
     /*   Each foot of length has a vertical                               */
     /*   wall with square cross section of one                            */
     /*   square foot.                                                     */
     /*                                                                    */
     /*   The heights of the walls, in feet, >=                            */
     /*   0, are inputs to the program.                                    */
     /*                                                                    */
     /*   Water is poured into the lake to fill                            */
     /*   it.  The water is stored between                                 */
     /*   walls.                                                           */
     /*                                                                    */
     /*   The puzzle is to find the volume, in                             */
     /*   cubic feet, of the water in the lake.                            */
     /*                                                                    */
     /*   For i = 1, 2, ..., n, the wall at i                              */
     /*   has height a.i.                                                  */
     /*                                                                    */
     /*   For our solution, first we find k so                             */
     /*   that a.k >= a.i for all i.  Second we                            */
     /*   go to the left end and work toward                               */
     /*   the wall at k finding lake volumes.                              */
     /*   Then go the the right end and work                               */
     /*   toward wall at k finding the rest of                             */
     /*   the lake volumes.                                                */
     /*                                                                    */
     /*   It's an O(n) solution.                                           */
     /*              ...