Solution (source code)

= Solution

The event includes stopping positions zero through $k$. Applying <Bayes' theorem> to their joint and marginal <probabilities> gives
$$
\boxed{P(G\mid X\le x_k)=\frac{p\sum_{j=0}^kg_j}{p\sum_{j=0}^kg_j+(1-p)\sum_{j=0}^kd_j}.}
$$
This <conditional probability> is defined whenever its conditioning event has positive <probability>. At $k=n$, it reduces to $p$.