Structural transition and uranium valence change in UTe$_2$ at high pressure revealed by x-ray diffraction and spectroscopy