Predict the Landslide Landslides

In this challenge, your job is to predict the extent of damage caused by a massive landslide.
We use the following simplified two-dimensional model for it, parameterized by an initial height h &gt;= 0 and a critical coefficient c &gt; 0.
You start with a cliff of height h, and it is assumed that the terrain is completely flat infinitely to the left and to the right of it.
For h = 6, the situation looks like this:

##########
##########
##########
##########
##########
##########
-----------------------


The - are immovable bedrock, and the # are unstable soil.
If the height difference between two neighboring columns is more than c, a landslide occurs: the top c units of soil from the left column fall down to the next c columns on the right, one to each.
The rightmost non-empty column in the figure is unstable for c = 2, so a landslide is triggered:

#########
#########
##########
##########
##########
############
-----------------------


The column is still unstable, which causes a second landslide:

#########
#########
#########
#########
############
############
-----------------------


Now, the column on its left has become unstable, so a new landslide is triggered there:

########
########
#########
###########
############
############
-----------------------


After this, the cliff is stable again.
The nice thing about this model is that the order in which the landslides are processed does not matter: the end result is the same.

The Task

Your program is given the integer parameters h and c as inputs (the order does not matter, but you must specify it on your answer), and it should output the total number of columns that the landslide affects.
This means the number of columns in the resulting stable cliff whose height is strictly between 0 and h.
In the above example, the correct output is 4.

You can write a full program or a function.
The lowest byte count wins, and standard loopholes are disallowed.

Test Cases

These are given in the format h c -&gt; output.

0  2  -&gt; 0
2  3  -&gt; 0
6  2  -&gt; 4
6  6  -&gt; 0
10 1  -&gt; 10
15 1  -&gt; 14
15 2  -&gt; 11
15 3  -&gt; 6
40 5  -&gt; 16
80 5  -&gt; 28
80 10 -&gt; 17
CJam, 62 57 bytes

As far as I can see, this is completely different approach to implement the solution from aditsu's answer.

q~:C;:HaH)*H){(:I\_@&gt;2&lt;:-C&gt;{I0a*C~)+C1a*+]z1fb_,}I?}h-H-,


Input goes in the form of h c

Example:

80 5


Output:

28


How it works

The logic is pretty straight forward here with a few tricks used to reduce code size.


Get h + 1 (+ 1 for h = 0 case) length array with each element being h to represent the cliff
Start iterating from the right most index of this array

If the two elements from current index have different more than c

Remove c from the current index element
Add 1 to next c elements of the array from the current index
Make the current index equal to the length of this new array
This makes sure that we stabilize the stones to the right of the current index first

else, reduce the current index

When we hit the left most index, we are making sure that all adjacent indexes have less than or equal to c difference
Remove any 0 or h value from the array and get length.


Code expansion

q~:C;:HaH)*H){(:I\_@&gt;2&lt;:-C&gt;{I0a*C~)+C1a*+]z1fb_,}I?}h-H-,
q~:C;:HaH)*H)
q~:C;:H                  "Read the input, evaluate it, store height in H and coeff. in C";
       aH)*              "Wrap the height number in an array and repeat it H + 1 times";
           H)            "Put H+1 on stack, representing the current index of iteration";
{(:I\_@&gt;2&lt;:-C&gt;{I0a*C~)+C1a*+]z1fb_,}I?}h
(:I\_@&gt;2&lt;:-C&gt;
(:I                      "Decrement the current index and store it in I";
   \_                    "Swap to put array on top and make 1 copy";
     @&gt;2&lt;                "Get the two elements starting from Ith index";
         :-              "Get the difference. The best part of this approach is that";
                         "For the right most index, where there is only element, it";
                         "returns the element itself, which is the expected difference";
           C&gt;            "Check if difference is greater than C";
{I0a*C~)+C1a*+]z1fb_,}   "This block will be executed when the difference is more than C";
 I0a*                    "Get an array of I length and all elements 0";
     C~)+                "Get -C value and append it to the above array";
         C1a*+           "Get C length array of 1s and concat with the above array";
              ]          "Wrap the two arrays, the cliff and the above one in an array";
               z1fb      "Transpose to get number pairs and add those pairs. For example";
                         "If we are at the right most index with H = 80 and C = 5,";
                         "The right section of the cliff looks like:";
                         "[ ... 80 80 80 80 80] and the array created in above step";
                         "looks like [ ... 0 0 0 0 -5 1 1 1 1 1]. After z, we have:";
                         "[ ... [80 0] [80 0] [80 0] [80 0] [80 -5] [1] [1] [1] [1] [1]]";
                         "After 1fb we get [ ... 80 80 80 80 75 1 1 1 1 1]";
                   _,    "Take a copy of the above resultant array and take its length";
I?                       "If difference was not greater than C, put I on stack";
                         "Now we either have the decremented index or new array length";
                         "on stack."
{ ... }h                 "This is a do while loop which makes sure that we iterate to";
                         "the left of the array. This loops runs till the top stack";
                         "element is 0 while not popping the top element";
        -H-,             "After the loop, we have the final cliff array and 0 on stack";
                         "Remove any 0 elements from the array, then remove any H";
                         "elements from the array and then take length to get the";
                         "number of columns which were modified";


Try it online here