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

Published on 12 May 2024 09:40 PM

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

Source code in the autohotkey programming language

WCBT(oTwr){
	topL := Max(oTwr*), l := num := 0, barCh := lbarCh := "", oLvl := []
	while (++l <= topL)
		for t, h in oTwr
			oLvl[l,t] := h ? "██" : "≈≈" , oTwr[t] := oTwr[t]>0 ? oTwr[t]-1 : 0
	for l, obj in oLvl{
		while (oLvl[l, A_Index] = "≈≈")
			oLvl[l, A_Index] := "  "
		while (oLvl[l, obj.Count() +1 - A_Index] = "≈≈")
			oLvl[l, obj.Count() +1 - A_Index] := "  "
		for t, v in obj
			lbarCh .= StrReplace(v, "≈≈", "≈≈", n), num += n
		barCh := lbarCh "`n" barCh, lbarCh := ""
	}
	return [num, barCh]
}


data := [[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]]
 
result := ""
for i, oTwr in data{
	inp := ""
	for i, h in oTwr
		inp .= h ", "
	inp := "[" Trim(inp, ", ") "]"
	x := WCBT(oTwr)
	result .= "Chart " inp " has " x.1 " water units`n" x.2 "------------------------`n"
}
MsgBox % result


  

You may also check:How to resolve the algorithm Shell one-liner step by step in the min programming language
You may also check:How to resolve the algorithm Get system command output step by step in the Stata programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the Wren programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the J programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Maxima programming language