Bash Stone Blocks
stone_blocks() {
local -n _h="$1"
local -a _height
local _index=0 _blocks=0
local _i
for _i in "${_h[@]}"; do
while (( _index > 0 && _height[_index-1] > _i )); do
((_index--))
done
if (( _index > 0 && _height[_index-1] == _i )); then
continue
fi
_height[_index]=$_i
((_blocks++))
((_index++))
done
echo "$_blocks"
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.