Gears
Time limit2sMemory limit512 MB
Simulate K gear turns; each turn makes a named gear rotate and may propagate to neighbors when the touching poles differ, then print a weighted score from the four top teeth.
- Level
Medium4 of 10
- Topics
- Implementation, Simulation, Array
- Solved
- No attempts yet
Problem
Four gears, each with 8 teeth, are placed in a row as in the picture below. Every tooth is either an N pole or an S pole. The leftmost gear is gear 1, and the gears to its right are gear 2, gear 3, and gear 4.

You now turn the gears K times. One turn moves a gear by exactly one tooth, either clockwise or counterclockwise.


Each turn names the gear to turn and the direction. For two neighboring gears, the touching teeth are the 3 o'clock tooth of the left gear and the 9 o'clock tooth of the right gear. When gear A turns and the two touching teeth of gear A and its neighbor B have different poles, B turns in the direction opposite to A. If the two poles are the same, B does not turn, and a gear beyond a gear that did not turn does not turn either. The poles are compared in the state that holds right before this turn starts.
Look at the state below.

The touching parts are circled with a green dotted line. If gear 3 turns counterclockwise here, gear 4 turns clockwise. Gear 2 does not turn, because the teeth it shares with gear 3 are both S poles, and gear 1 does not turn because gear 2 did not turn. The result is the picture below.

If gear 1 then turns clockwise, gear 2 turns counterclockwise, and because gear 2 turns, gear 3 turns clockwise at the same time. Gear 4 does not turn, because the teeth it shares with gear 3 have the same pole. The result is the picture below.

Given the initial state of the gears and the list of turns, write a program that computes the total score after all turns are done.
Input
The first four lines give the state of gear 1, gear 2, gear 3, and gear 4, one gear per line. Each state is a string of 8 characters, each 0 or 1, listed from the 12 o'clock tooth in clockwise order. An N pole is 0 and an S pole is 1.
The fifth line has the number of turns ().
Each of the next lines describes one turn, in order. A line holds two integers. The first is the number of the gear to turn (), and the second is the direction. A direction of 1 means clockwise, and -1 means counterclockwise.
Output
Print the total score of the four gears after all turns, on one line. The score is computed like this.
- Gear 1 scores 0 if its 12 o'clock tooth is an N pole, and 1 if it is an S pole
- Gear 2 scores 0 if its 12 o'clock tooth is an N pole, and 2 if it is an S pole
- Gear 3 scores 0 if its 12 o'clock tooth is an N pole, and 4 if it is an S pole
- Gear 4 scores 0 if its 12 o'clock tooth is an N pole, and 8 if it is an S pole