Solution (source code)

= Solution

In the shock rest frame the upstream and downstream speed magnitudes are $U$ and $U-V$. Denote the downstream density by $\rho_2$, and write $v_i=1/\rho_i$ for specific volume. The <Rankine-Hugoniot conditions for a perfect gas> express conservation of mass, momentum and total energy:
$$
j=\rho_1U=\rho_2(U-V),\qquad p_1+j^2v_1=p_2+j^2v_2,
$$
$$
e_1+p_1v_1+\frac12j^2v_1^2=e_2+p_2v_2+\frac12j^2v_2^2,
\qquad e_i=\frac{p_iv_i}{\gamma-1}.
$$
These follow by balancing the respective fluxes across a stationary infinitesimal control volume around the <shock wave>. The mass relation also gives $V=j(v_1-v_2)$, and momentum gives $j^2(v_1-v_2)=p_2-p_1$.

Eliminate the kinetic-energy difference in the energy condition using momentum:
$$
e_2-e_1=p_1v_1-p_2v_2+\frac12(p_2-p_1)(v_1+v_2)
=\frac12(p_1+p_2)(v_1-v_2).
$$
Substitute the given perfect-gas internal energy and collect the coefficients of $v_1,v_2$:
$$
[(\gamma+1)p_2+(\gamma-1)p_1]v_2
=[(\gamma-1)p_2+(\gamma+1)p_1]v_1.
$$
Hence
$$
v_1-v_2=\frac{2(p_2-p_1)}{\rho_1[(\gamma+1)p_2+(\gamma-1)p_1]}.
$$
Finally $V^2=j^2(v_1-v_2)^2=(p_2-p_1)(v_1-v_2)$ gives
$$
\boxed{V^2=\frac{(p_2-p_1)^2}{\rho_1[(\gamma+1)p_2/2+(\gamma-1)p_1/2]}.}
$$
For the advancing piston choose the compressive branch $p_2>p_1$, $U>V>0$; the algebraic squared relation alone does not specify that branch.