Totalling Troublesome T's

JavaScript (ES6), 32 30 bytes

Saved 2 bytes thanks to @Shaggy

(w,h,x)=>Q=n=>--h?n+Q(n-x):n*w

Takes input in a curried format: f(W,H,x,y)(n)

let f =
(w,h,x)=>Q=n=>--h?n+Q(n-x):n*w;

console.log(
  f(3, 4, 9, 9)(32)
);

How?

First we note that the sum of the T starting at n with height H can be broken down into two sums:

  • n
  • The sum of the T starting one row higher with height H - 1

By repeatedly adding n to the total, moving one row up, and subtracting 1 from H until it reaches 1, we end up summing the vertical stem of the T. Moving one row up is accomplished by subtracting x from n, since it can be observed that the difference between any cell and the one above is x.

When H reaches 1, we now have only the crossbar of width W left to sum. At this point n represents the center of the crossbar. We could sum the crossbar recursively as we did with the stem, but instead we take advantage of a fairly simple formula:

sum(n - a : n + a) = (n - a) + (n - (a-1)) + ... + n + ... + (n + (a-1)) + (n + a)
                   = (n - a) + (n + a) + (n - (a-1)) + (n + (a-1)) + ... + n
                   = 2n + 2n + ... + n
                   = n * (2a + 1)

In this case, our a is (W - 1) / 2, which makes

n * (2a + 1) = n * (((W - 1) / 2) * 2 + 1)
             = n * ((W - 1) + 1)
             = n * W

which is the sum of the crossbar.


C# (Visual C# Compiler), 64 52 bytes

(x,y,W,H,n)=>{for(y=0;H-->1;n-=x)y+=n;return y+W*n;}

Try it online!

The non-recursive answer did indeed turn out significantly shorter. Gross misuse of for loops and the fact that y is officially a mandatory input even though it's not used.


Python 3, 38 bytes

lambda x,W,H,n:~-H*(x*(1-H/2-W)+n)+W*n

Try it online!

-4 bytes thanks to Jonathan Allan
-4 bytes thanks to Kevin Cruijssen/Ayb4btu