..........what the hell, my diagram got ****ed up when I posted...so we will have to explain
The longest pipe will have the length equal to the minimum of where x is the distance from the top of the pipe in the first hallway to the edge where the hallways meet, and y is the distance from the edge where the hallways meet to the point where the pipe hits the wall. The angle from the horizontal of the second hallway to the line defined by y is going to be theta.
Thus the minimum must occur at
Let us go back to,
we can see that (by constructing the triangle and using pythag),