Rendezvous on a Tetrahedron
Time limit1sMemory limit512 MB
Two worms start at vertex A of a regular tetrahedron, crawl straight across faces reflecting off edges, and stop after integer trail lengths; decide whether they end on the same face.
- Level
Hard8 of 10
- Topics
- Geometry, Implementation, Math
- Solved
- No attempts yet
Problem
Two worms, P and Q, crawl on the surface of a regular tetrahedron with vertices A, B, C and D. Both worms start at the vertex A, move straight ahead, and stop after a while.
When a worm reaches an edge of the tetrahedron, it moves onto the adjacent face and keeps going, holding the same angle with the edge it just crossed (Figure 1).
Write a program that decides whether P and Q were on the same face of the tetrahedron when they stopped.
Each worm is a point with no length, area or volume.

Figure 1. Crossing an edge
The length of each trail is an integer multiple of the unit length, where the unit length is the edge length of the tetrahedron. Apart from its start point and the neighborhood of that point, every trail stays more than 0.001 unit away from every vertex, so each worm crossed at least one edge. Both worms stopped more than 0.001 unit away from every edge.
The initial direction of a worm is given by two values. One is the edge XY, the first edge the worm met after its start. The other is the angle in degrees between the edge AX and the direction of the worm.

Figure 2. Trails of the two worms in the first example, where P crosses the edge CD with and , and Q crosses the edge DB with and .
In that example P goes over the edge CD and stops on the face opposite the vertex A, and Q goes over the edge DB and stops on the same face.
Input
The input is a single test case in the following format.
XPYP dP lP
XQYQ dQ lQ
The first line describes worm P and the second line describes worm Q. Each line holds the edge name, the angle and the length, separated by spaces. The edge name is written as two letters with no space between them.
(W is P or Q) is the first edge that worm W crossed after its start. It is one of BC, CD, DB.
The integer () is the angle in degrees between the edge and the initial direction of worm W on the face .
The integer () is the length of the trail that worm W left on the surface, measured in unit lengths.
Output
Print YES if the two worms stopped on the same face of the tetrahedron. Otherwise print NO.