Code:

A:@a....y....[1]...40-y...[3]
B:@b................40+y...............[2]

A=back of army line (speed = a), B=horse position (speed = b)

A travels distance y, to position [1]

B travels 40 miles further (to end of army line) to position [2]

A arrives at [1] at same time as B arrives at [2]

A continues such that it travels total of 40, to position [3]

B comes back, to position [3] : both get there at same time

You now have enough info to get b = a[1 + SQRT(2)]

From that, you can calculate y

Distance by horse = x + 2y

That's all I'll give you; don't wanna spoil your fun of getting b, then y