In a secret lab corridor, there are emitters and gates: A number represents a laser emitter with a certain frequency, a 0
represents an inactive gate. If two emitters of the same frequency face each other with only inactive gates (0
) in between, their beams meet and activate the entire stretch of gates in between. But if any other emitter is in the way, the beam is blocked.
Your task
Write a program that simulates the beam activation.
Input
A list/array of nonnegative integers.
Output
The transformed list/array, with activated gates.
Winning Criteria
The shortest correct program (measured in bytes) wins. In the event of a tie, the earliest posted answer wins.
Sample data
Example 1 (Simple case)
[0,2,0,0,2]
→ [0,2,2,2,2]
Example 2 (Gate between emitters)
[0,2,0,1,0,2]
→ [0,2,0,1,0,2]
Example 3 (A more complex example)
[0,2,0,2,5,0,0,5,1]
→ [0,2,2,2,5,5,5,5,1]
Example 4 (Consecutive emitters)
[3,0,0,3,0,3]
→ [3,3,3,3,3,3]