Moving Walls

I’m working on a room inmy game which has some boxes(walls) that move back and forth blocking your way throgh the room. So what I have to do it place a box in between the 2 walls that are closing the way and jump over it while it holds the walls.
In a diagram it looks like this:

Diagram(Click Here)

XX → is a THE BOX I must place in between the walls
1 → start position of the walls
3 → is where it would go if there was no box blocking the way, the way would be closed
2 → is where it should stop if the box is in the way

The solution I found to get this to work is to calculate the distance traveled, so if the walls hit the box before it gets to (3), it would only move to (2) and push the box to the middle of the room(the box(XX) would stay at the place(33).

But I need help with codes, How do I calculate that distance? using a Vector3 magnitude? a single axis dislocation?

Need some help, btw any code must be in C#, Thanks.

You can calculate the distance using Vector3.Distance(a, b)