In order to solve this problem it will prove to be useful to have a function which takes as input a pair (x,y) and finds the x-coordinate of the point on the unit circle at which a tangent line can be drawn passing through the point (x,y). Actually, there are always two such lines so two functions will be needed.

To start, the following equation is obtained by taking an arbitrary point (x,y) and a point (p,q) on the unit circle such that the line connecting these two points is tangent to the unit circle. The slope of the tangent line at (p,q) is -p/q from simple trigonometry. Equating this to the slope of the line segment connecting the two points yields the equation.

Solving this equation for p will yield the x coordinate of the point at which the line passing through (x,y) is tangent to the unit circle. After some Maple manipulations this will become the function discussed above.

The two functions which yield the x-coordinate of the points at which the tangent to the unit circle passing through (x,y) can now be easily defined. They will be called XofTangent1 and XofTangent2.

The equation has two solutions. This should be apparent from the geometry since the line passing from (x,y) tangent to the circle can pass on either side of the circle - hence the two solutions. Which solution is best for our purposes depends on the point (Q1,Q2). If it is in the upper right quadrant then the x-coordinate of the tangent line passing below the circle will be greater than that of the tangent line passing above the circle. In this case XofTangent1 will be used. If the point (Q1,Q2) falls in the lower right quadrant then XofTangent2 will be used.

Before continuing, the point Q1 and Q2 will be defined. These will be used to find points (P1U,P2U) and (P1L,P2L) which are the points at which the line tangent to the circle and passing through (Q1,Q2) touches the circle. The point (P1U,P2U) arises when (Q1,Q2) is the upper right quadrant while (P1L,P2L) arises when (Q1,Q2) is in the lower right quadrant.

The next step is to find the point on the y axis at which the y-axis is intersected by the line tangent to the circle passing through (Q1,Q2). This point will be denoted by (0, z). It is easily found by calculating slopes and equating them. This will first be done only for the case when (Q1,Q2) is in the upper right quadrant and XofTangent1 is used.

The length of the line segment is obtained by subtracting from z the height of the end of the line segment extending from the circular ditch.

The function denoted by LineSegment is now defined to be equal to the length just found.

The total length of the ditch is now easily defined by adding to LineSegment the length of the circular ditch and the length of the two line segments attached to it. Call this function DitchLengthU to emphasize it is only valid when (Q1,Q2) is in the upper right quadrant.

The total ditch length in the case that (Q1,Q2) is in the lower right
quadrant will now be considered. It is entirely analogous to the first
case.

The length of the "U" shaped ditch mentioned in the problem statement has length Pi + 2. The value of this is found below.

From the graphs considered, it is natural to look at the folllowing value of DitchLengthL:

Hence, digging a ditch with these parameters does even better than the "U" shaped ditch. The only thing which remains to be checked is that these parameters do indeed yield a dicth where (Q1,Q2) lies in the lower right quadrant. Recall that this simple means that Q2 is negative. Hence it suffices to substitue the parameters into this expression and see what happens.

The negative value confirms that this is a valid set of parameters.