Solution (source code)

= Solution

Let $q_j=\mathbb P(M_j=Y)$. By the <law of total probability>,
$$
p_n=\frac12p_{n-1}+\frac38q_{n-1}.
$$
There is no return contribution from the <absorbing state> $Z$. Since $Y$ can be entered only from $X$, $q_{n-1}=\frac12p_{n-2}$ for $n\geq2$. Substitution yields
$$
\boxed{16p_n=8p_{n-1}+3p_{n-2},\qquad n\geq2.}
$$
No independence between successive positions is assumed; the conditional transitions give the recurrence.