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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Water collected between towers step by step in the Julia 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 Julia programming language

This Julia program calculates the amount of water that can be collected between a series of towers. Here's a detailed explanation:

  1. watercollected Function:

    • This function takes a vector of integers towers representing the heights of the towers.
    • It calculates the maximum height of the towers to the left (high_lft) and right (high_rgt) of each tower.
    • The water level at each tower is the minimum of these two maximum heights minus the current tower's height, excluding negative values.
    • The function returns a vector of integers representing the water levels for each tower.
  2. towerprint Function:

    • This function visualizes the towers and the water levels.
    • It creates copies of the towers and levels vectors to avoid modifying the original input.
    • It prints the tower and water levels for each row, from the highest height to the lowest.
    • The output shows the towers as NN for filled cells, ≈≈ for water cells, and for empty cells.
  3. Main Program:

    • The program defines a list of test cases, where each list represents the heights of towers.
    • For each test case, it calculates the water levels using the watercollected function and visualizes the results using the towerprint function.

Here's an example of the output for one of the test cases:

  3 | NN NN NN ≈≈ NN NN NN NN NN NN NN NN NN NN NN |
  2 | NN NN NN ≈≈ NN NN NN NN NN NN NN NN NN NN NN |
  1 | NN NN ≈≈ ≈≈ NN NN NN NN NN NN NN NN NN NN NN |: Water lvl
  0 | NN ≈≈ ≈≈ ≈≈ NN NN NN NN NN NN NN NN NN NN NN |: Tower lvl

This output shows that the first three towers have water levels of 3, 2, and 1, respectively, while the last four towers have no water collected.

Source code in the julia programming language

using Printf

function watercollected(towers::Vector{Int})
    high_lft = vcat(0, accumulate(max, towers[1:end-1]))
    high_rgt = vcat(reverse(accumulate(max, towers[end:-1:2])), 0)
    waterlvl = max.(min.(high_lft, high_rgt) .- towers, 0)
    return waterlvl
end

function towerprint(towers, levels)
    ctowers = copy(towers)
    clevels = copy(levels)
    hmax = maximum(towers)
    ntow = length(towers)
    for h in hmax:-1:1
        @printf("%2i |", h)
        for j in 1:ntow
            if ctowers[j] + clevels[j]  h
                if clevels[j] > 0
                    cell = "≈≈"
                    clevels[j] -= 1
                else
                    cell = "NN"
                    ctowers[j] -= 1
                end
            else
                cell = "  "
            end
            print(cell)
        end
        println("|")
    end


    println("   " * join(lpad(t, 2) for t in levels) * ": Water lvl")
    println("   " * join(lpad(t, 2) for t in towers) * ": Tower lvl")
end

for towers in [[1, 5, 3, 7, 2], [5, 3, 7, 2, 6, 4, 5, 9, 1, 2],
    [2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1],
    [5, 5, 5, 5], [5, 6, 7, 8], [8, 7, 7, 6], [6, 7, 10, 7, 6]]
    towerprint(towers, watercollected(towers))
    println()
end


  

You may also check:How to resolve the algorithm Day of the week step by step in the Java programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Swift programming language