and why does the last Medusa attack the farthest unit #1 instead of killing the closest one?
The last stack of Medusas goes through the wall, not to unit #2, but to unit #1. For some reason.
The theory is: when the two targets are of EQUAL value, the monster will prefer hitting the target on the #1 direction (x - axis) than the #6 direction (z - axis). Examples for monsters facing right are provided in the graphs below.


The Medusa in this particular example is facing left, and you will need to reverse the x and z axis to understand its movements and priorities.

This theory is still under investigation but hasn't been proved wrong.